Youtube API V3 and Etag

Webmaster picture Webmaster · Feb 13, 2014 · Viewed 15.5k times · Source

I use the youtube api v3 and i would like to understand how does the Etag. I would like to use it for what it takes to cache purpose but I do not know what to do in PHP. Could you tell me the steps to follow once the etag recovered ? please. Thanks for help.

Answer

Redtopia picture Redtopia · Mar 25, 2014

According to the youtube docs (https://developers.google.com/youtube/v3/getting-started#etags), an eTag is basically used to determine if a resource has changed. Use them for:

  1. Optimization - Caching youtube resources in your app can reduce bandwidth and latency. When caching, store the eTag so that you can include it when getting a resource. If the resource has not changed, you will get a 304 response code (NOT MODIFIED), which means you can use your cached version. Otherwise, you will get the updated version of the resource.

  2. Quota Usage - You can reduce the amount you tap into your quota by caching youtube data. The first time you get the resource, you will tap into your quota. Before displaying the resource, first check to see if your cached resource has changed, which will only cost you 1 quota unit. If the resource has not changed, youtube will return a 304 response. If it has changed, you can get the resource again, costing various quota units depending on what you are getting. For more on your quota: (https://developers.google.com/youtube/v3/getting-started#quota).

  3. Overwrite protection - If you are overwriting a resource, including the eTag will ensure that you are not overwriting a newer version of the resource.

eTags are part of the HTTP 1.1 spec (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19) and are used in the headers of the request/response. Here's a good article that talks about them at a low level: http://www.ibuildings.com/blog/2013/07/etags-uninitiated

As far as using eTags in PHP, I can only suggest a couple things since I've never done it. YouTube returns eTags for feeds AND individual items within a feed, and I'm not sure how to use them for individual items within a feed. But to get the original feed itself, essentially you would use curl and add the eTag to the header of your request (PHP cURL custom headers). You might also want to check out http_cache_etag (http://www.php.net/manual/en/function.http-cache-etag.php)