Posted By

TimoZachi on 09/10/11


Tagged

width video thumbnail api height duration YouTube


Versions (?)

Get All information from youtube video


 / Published in: PHP
 

This code snippet gets all information from a YouTube video (title, description, duration, thumbnail url, thumbnail width, thumbnail height, etc..) using the video id and YouTube API.

  1. <?php
  2. //The Youtube's API url
  3. define('YT_API_URL', 'http://gdata.youtube.com/feeds/api/videos?q=');
  4.  
  5. //Change below the video id.
  6. $video_id = '66Wi3isw3NY';
  7.  
  8. //Using cURL php extension to make the request to youtube API
  9. $ch = curl_init();
  10. curl_setopt($ch, CURLOPT_URL, YT_API_URL . $video_id);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  12. //$feed holds a rss feed xml returned by youtube API
  13. $feed = curl_exec($ch);
  14. curl_close($ch);
  15.  
  16. //Using SimpleXML to parse youtube's feed
  17. $xml = simplexml_load_string($feed);
  18.  
  19. $entry = $xml->entry[0];
  20. //If no entry whas found, then youtube didn't find any video with specified id
  21. if(!$entry) exit('Error: no video with id "' . $video_id . '" whas found. Please specify the id of a existing video.');
  22. $media = $entry->children('media', true);
  23. $group = $media->group;
  24.  
  25. $title = $group->title;//$title: The video title
  26. $desc = $group->description;//$desc: The video description
  27. $vid_keywords = $group->keywords;//$vid_keywords: The video keywords
  28. $thumb = $group->thumbnail[0];//There are 4 thumbnails, the first one (index 0) is the largest.
  29. //$thumb_url: the url of the thumbnail. $thumb_width: thumbnail width in pixels.
  30. //$thumb_height: thumbnail height in pixels. $thumb_time: thumbnail time in the video
  31. list($thumb_url, $thumb_width, $thumb_height, $thumb_time) = $thumb->attributes();
  32. $content_attributes = $group->content->attributes();
  33. //$vid_duration: the duration of the video in seconds. Ex.: 192.
  34. $vid_duration = $content_attributes['duration'];
  35. //$duration_formatted: the duration of the video formatted in "mm:ss". Ex.:01:54
  36. $duration_formatted = str_pad(floor($vid_duration/60), 2, '0', STR_PAD_LEFT) . ':' . str_pad($vid_duration%60, 2, '0', STR_PAD_LEFT);
  37.  
  38. //echoing the variables for testing purposes:
  39. echo 'title: ' . $title . '<br />';
  40. echo 'desc: ' . $desc . '<br />';
  41. echo 'video keywords: ' . $vid_keywords . '<br />';
  42. echo 'thumbnail url: ' . $thumb_url . '<br />';
  43. echo 'thumbnail width: ' . $thumb_width . '<br />';
  44. echo 'thumbnail height: ' . $thumb_height . '<br />';
  45. echo 'thumbnail time: ' . $thumb_time . '<br />';
  46. echo 'video duration: ' . $vid_duration . '<br />';
  47. echo 'video duration formatted: ' . $duration_formatted;
  48. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: zoltan on September 25, 2012

Hi! Why am I getting this error only sometimes? Fatal error: Call to a member function children() on a non-object

Posted By: TimoZachi on September 26, 2012

Zoltan, the reason the error message appeared it's probably because the video id you used isn't a valid youtube id (there are no videos with that id). You need to specify an EXISTENT video id. I've updated the snippet so that it shows a more descriptive error message in case that happens again. Sry bad english.

Posted By: TimoZachi on September 26, 2012

Zoltan, the reason the error message appeared it's probably because the video id you used isn't a valid youtube id (there are no videos with that id). You need to specify an EXISTENT video id. I've updated the snippet so that it shows a more descriptive error message in case that happens again. Sry bad english.

Posted By: zoltan on October 8, 2012

It seems that I had problems with internet connection, now works fine. Thanks a lot for answer and thank you for code update!

Posted By: sumeetkashyap on June 14, 2013

There is a problem with the script. It would not return information for some videos specially the ones which have been added recently. Using this endpoint would fix the issue but you need to change your code: http://gdata.youtube.com/feeds/api/videos/[ID_VIDEO]?v=2&alt=json

You need to login to post a comment.