Get YouTube Video Data with PHP

This is amazing simple. I always thought oh no another API and if its anything like Facebooks API then you have to learn something new every month! But this is amazingly easy!

You just need to pull the information you want from You Tube via ‘simplexml‘ in PHP

YouTube has loads of different RSS feeds, in this case I am just using the one that gets all data about a single video.

[php]
//your video id
$v = YOUR_VIDEO_ID

// get xml
$video_feed = file_get_contents("http://gdata.youtube.com/feeds/api/videos/$v");
$sxml = new SimpleXmlElement($video_feed);

//set up nodes
$namespaces = $sxml->getNameSpaces(true);
$media = $sxml->children($namespaces[‘media’]);
$yt = $media->children($namespaces[‘yt’]);
$yt_attrs = $yt->duration->attributes();

//vars
$video_title = $sxml->title;
$video_description = nl2br($sxml->content);
$video_keywords = $media->group->keywords;
$video_length = $yt_attrs[‘seconds’];
[/php]