
Using SimpleXML we will parse the Youtube feed to show today’s top videos.
The feed link is:
http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?time=today&start-index=1&max-results=5
it contains today’ s top 5 videos. If you want to know more about the Youtube Gdata Api you can start by reading the API Reference Guide.
This is the feed structure:
<?xml version='1.0' encoding='UTF-8'?> <feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' gd:etag='W/"DkYDQH47eCp7ImA9WxBREko."'> <id>tag:youtube.com,2008:standardfeed:us:top_rated</id> <updated>2009-12-31T07:42:51.000-08:00</updated> <category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/> <title>Top Rated</title> <logo>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</logo> <link rel='alternate' type='text/html' href='http://www.youtube.com/browse?s=tr'/> <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?client=ytapi-google-jsdemo'/> <link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/us/top_rated/batch?client=ytapi-google-jsdemo'/> <link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=2&max-results=1&time=today&client=ytapi-google-jsdemo'/> <link rel='service' type='application/atomsvc+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/us/top_rated?alt=atom-service'/> <link rel='previous' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=1&max-results=1&time=today&client=ytapi-google-jsdemo'/> <link rel='next' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=3&max-results=1&time=today&client=ytapi-google-jsdemo'/> <author> <name>YouTube</name> <uri>http://www.youtube.com/</uri> </author> <generator version='2.0' uri='http://gdata.youtube.com/'>YouTube data API</generator> <openSearch:totalResults>100</openSearch:totalResults> <openSearch:startIndex>2</openSearch:startIndex> <openSearch:itemsPerPage>1</openSearch:itemsPerPage> <entry gd:etag='W/"D0UERX47eCp7ImA9WxBREko."'> <id>tag:youtube.com,2008:video:9DTWIYIgkrk</id> <published>2009-12-30T22:36:23.000Z</published> <updated>2009-12-31T16:00:04.000Z</updated> <category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/> <category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Entertainment' label='Entertainment'/> <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='shaycarl'/> <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='shaytards'/> <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='costco'/> <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='love'/> <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='the'/> <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='smell'/> <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='of'/> <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='new'/> <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='balls'/> <title>RACING THROUGH COSTCO! (12/29/09-300th!!!)</title> <content type='application/x-shockwave-flash' src='http://www.youtube.com/v/9DTWIYIgkrk?f=standard&c=ytapi-google-jsdemo&app=youtube_gdata'/> <link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=9DTWIYIgkrk&feature=youtube_gdata'/> <link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/9DTWIYIgkrk/responses?client=ytapi-google-jsdemo'/> <link rel='http://gdata.youtube.com/schemas/2007#video.related' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/9DTWIYIgkrk/related?client=ytapi-google-jsdemo'/> <link rel='http://gdata.youtube.com/schemas/2007#mobile' type='text/html' href='http://m.youtube.com/details?v=9DTWIYIgkrk'/> <link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/us/top_rated/v/9DTWIYIgkrk?client=ytapi-google-jsdemo'/> <author> <name>SHAYTARDS</name> <uri>http://gdata.youtube.com/feeds/api/users/shaytards</uri> </author> <gd:comments> <gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/9DTWIYIgkrk/comments?client=ytapi-google-jsdemo' countHint='2837'/> </gd:comments> <media:group> <media:category label='Entertainment' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Entertainment</media:category> <media:content url='http://www.youtube.com/v/9DTWIYIgkrk?f=standard&c=ytapi-google-jsdemo&app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='731' yt:format='5'/> <media:content url='rtsp://v5.cache3.c.youtube.com/CjkLENy73wIaMAm5kiCCIdY09BMYDSANFEITeXRhcGktZ29vZ2xlLWpzZGVtb0gGUghzdGFuZGFyZAw=/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='731' yt:format='1'/> <media:content url='rtsp://v7.cache4.c.youtube.com/CjkLENy73wIaMAm5kiCCIdY09BMYESARFEITeXRhcGktZ29vZ2xlLWpzZGVtb0gGUghzdGFuZGFyZAw=/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='731' yt:format='6'/> <media:credit role='uploader' scheme='urn:youtube' yt:type='partner'>SHAYTARDS</media:credit> <media:description type='plain'>My Twitter http://www.twitter.com/shaycarl My Dailybooth http://www.dailybooth.com/shaycarl</media:description> <media:keywords>shaycarl, shaytards, costco, love, the, smell, of, new, balls</media:keywords> <media:player url='http://www.youtube.com/watch?v=9DTWIYIgkrk&feature=youtube_gdata'/> <media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/default.jpg' height='90' width='120' time='00:06:05.500'/> <media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/2.jpg' height='90' width='120' time='00:06:05.500'/> <media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/1.jpg' height='90' width='120' time='00:03:02.750'/> <media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/3.jpg' height='90' width='120' time='00:09:08.250'/> <media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/hqdefault.jpg' height='360' width='480'/> <media:title type='plain'> RACING THROUGH COSTCO! (12/29/09-300th!!!)</media:title> <yt:aspectRatio>widescreen</yt:aspectRatio> <yt:duration seconds='731'/> <yt:uploaded>2009-12-30T22:36:23.000Z</yt:uploaded> <yt:videoid>9DTWIYIgkrk</yt:videoid> </media:group> <gd:rating average='4.973422' max='5' min='1' numRaters='13244' rel='http://schemas.google.com/g/2005#overall'/> <yt:statistics favoriteCount='1805' viewCount='302'/> </entry> </feed>
Now, I will load the xml file using simplexml_load_file:
$feed = "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=1&max-results=1&time=today"; $xml = simplexml_load_file($feed); echo '<pre>'; print_r($xml); echo '<pre>';
As you see I check the simplexml object structure using print_r($xml), this will help me to see how can I parse the xml response from youtube.
To get the informations for every video I will parse every node in the Youtube GData response, so the previous code becomes:
$feed = "http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=2&max-results=1&time=today"; // load xml $xml = simplexml_load_file($feed); // parse every node <entry> to get the video information foreach ($xml->entry as $video) { echo '<h2>'.$video->title.'</h2><br>'; // video title echo '<pre>'.$video->content.'</pre><br>'; // video content // media: namespace $media = $video->children('http://search.yahoo.com/mrss/'); echo 'Tags: '.$media->group->keywords.'<br>'; // video tags echo 'Video url: '.$media->group->player->attributes()->url.'<br>'; // video url echo 'Uploaded by '.$video->author->name.'<br>'; // author name echo '<hr>'; }
$video->title – gets the video title from the node “title”
$video->children(‘http://search.yahoo.com/mrss/’) – gets the nodes in media: namespace
$media->group->keywords – gets the video tags from the
$media->group->player->attributes()->url – gets the video url from the
This was only a simple example of using the incredible Youtube GData API, I hope you liked it.