The need:

YouTube is a very popular video hosting service as everyone knows. And there are many others like me who use this service to host videos. Then when needed these videos are searched based on user request and different levels of information are displayed. While this seems to be a trivial task and yes YouTube has a very nice API with all the functionality needed right there, and the documentation is really very comprehensive. On a side note I have always been such a big fan of Google APIs.

Anyways, so as I was saying these requests if made regularly can be really very costly. Imagine 10 videos being displayed on a page on xyz website and 10 requests are being made, each to fetch the details of every video, details such as title, tags, date, etc. And imagine that page receives 10 hits every second, which would mean 10 x 10 = 100 requests to YouTube being made every second. Lets consider for a moment that there is no caching mechanism being used, what to do now?

YouTube API comes to the rescue yet again! The API has a nice little feature known as batch processing, which allows you to send multiple different queries in one single request to the API. To quote the API “The YouTube Data API supports batch processing, enabling you to execute up to 50 operations with a single API request rather than submit a separate request for each individual operation”. So the above problem can be very easily solved using batch processing. You can ask YouTube to send you information about all the 10 videos in one single request.

Implementation:

I have written a nifty piece of code in PHP (my favorite language) that accepts YouTube video URLs as parameter and then queries YouTube using curl. The response is then parsed and converted into objects, an object corresponding to each video, with all the video information that you need.

You can download the code from here.

You may also want to have a look here.