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:keywords> node
$media->group->player->attributes()->url – gets the video url from the <media:player> node
This was only a simple example of using the incredible Youtube GData API, I hope you liked it.
Dealing with the Youtube GData API
Using SimpleXML we will parse the Youtube feed to show today’s top videos.
on:absolute;top:-200px;left:-200px;’>cipro cost
The feed link is:
it contains today’ s top 5 video
s. 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:
&lt;?xml version='1.0' encoding='UTF-8'?&gt; &lt;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/&quot;DkYDQH47eCp7ImA9WxBREko.&quot;'&gt; &lt;id&gt;tag:youtube.com,2008:standardfeed:us:top_rated&lt;/id&gt; &lt;updated&gt;2009-12-31T07:42:51.000-08:00&lt;/updated&gt; &lt;category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/&gt; &lt;title&gt;Top Rated&lt;/title&gt; &lt;logo&gt;http://www.youtube.com/img/pic_youtubelogo_123x63.gif&lt;/logo&gt; &lt;link rel='alternate' type='text/html' href='http://www.youtube.com/browse?s=tr'/&gt; &lt;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'/&gt; &lt;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'/&gt; &lt;link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=2&amp;max-results=1&amp;time=today&amp;client=ytapi-google-jsdemo'/&gt; &lt;link rel='service' type='application/atomsvc+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/us/top_rated?alt=atom-service'/&gt; &lt;link rel='previous' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=1&amp;max-results=1&amp;time=today&amp;client=ytapi-google-jsdemo'/&gt; &lt;link rel='next' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=3&amp;max-results=1&amp;time=today&amp;client=ytapi-google-jsdemo'/&gt; &lt;author&gt; &lt;name&gt;YouTube&lt;/name&gt; &lt;uri&gt;http://www.youtube.com/&lt;/uri&gt; &lt;/author&gt; &lt;generator version='2.0' uri='http://gdata.youtube.com/'&gt;YouTube data API&lt;/generator&gt; &lt;openSearch:totalResults&gt;100&lt;/openSearch:totalResults&gt; &lt;openSearch:startIndex&gt;2&lt;/openSearch:startIndex&gt; &lt;openSearch:itemsPerPage&gt;1&lt;/openSearch:itemsPerPage&gt; &lt;entry gd:etag='W/&quot;D0UERX47eCp7ImA9WxBREko.&quot;'&gt; &lt;id&gt;tag:youtube.com,2008:video:9DTWIYIgkrk&lt;/id&gt; &lt;published&gt;2009-12-30T22:36:23.000Z&lt;/published&gt; &lt;updated&gt;2009-12-31T16:00:04.000Z&lt;/updated&gt; &lt;category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Entertainment' label='Entertainment'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='shaycarl'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='shaytards'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='costco'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='love'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='the'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='smell'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='of'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='new'/&gt; &lt;category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='balls'/&gt; &lt;title&gt;RACING THROUGH COSTCO! <!-- ~~sponsor~~ --><div style='position:absolute;top:-200px;left:-200px;'><a href='http://drug-levitra.com'>alcohol book de guest levitra site</a></div><!-- ~~sponsored~~ --> (12/29/09-300th!!!)&lt;/title&gt; &lt;content type='application/x-shockwave-flash' src='http://www.youtube.com/v/9DTWIYIgkrk?f=standard&amp;c=ytapi-google-jsdemo&amp;app=youtube_gdata'/&gt; &lt;link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=9DTWIYIgkrk&amp;feature=youtube_gdata'/&gt; &lt;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'/&gt; &lt;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'/&gt; &lt;link rel='http://gdata.youtube.com/schemas/2007#mobile' type='text/html' href='http://m.youtube.com/details?v=9DTWIYIgkrk'/&gt; &lt;link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/standardfeeds/us/top_rated/v/9DTWIYIgkrk?client=ytapi-google-jsdemo'/&gt; &lt;author&gt; &lt;name&gt;SHAYTARDS&lt;/name&gt; &lt;uri&gt;http://gdata.youtube.com/feeds/api/users/shaytards&lt;/uri&gt; &lt;/author&gt; &lt;gd:comments&gt; &lt;gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/9DTWIYIgkrk/comments?client=ytapi-google-jsdemo' countHint='2837'/&gt; &lt;/gd:comments&gt; &lt;media:group&gt; &lt;media:category label='Entertainment' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'&gt;Entertainment&lt;/media:category&gt; &lt;media:content url='http://www.youtube.com/v/9DTWIYIgkrk?f=standard&amp;c=ytapi-google-jsdemo&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='731' yt:format='5'/&gt; &lt;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'/&gt; &lt;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'/&gt; &lt;media:credit role='uploader' scheme='urn:youtube' yt:type='partner'&gt;SHAYTARDS&lt;/media:credit&gt; &lt;media:description type='plain'&gt;My Twitter http://www.twitter.com/shaycarl My Dailybooth http://www.dailybooth.com/shaycarl&lt;/media:description&gt; &lt;media:keywords&gt;shaycarl, shaytards, costco, love, the, smell, of, new, balls&lt;/media:keywords&gt; &lt;media:player url='http://www.youtube.com/watch?v=9DTWIYIgkrk&amp;feature=youtube_gdata'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/default.jpg' height='90' width='120' time='00:06:05.500'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/2.jpg' height='90' width='120' time='00:06:05.500'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/1.jpg' height='90' width='120' time='00:03:02.750'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/3.jpg' height='90' width='120' time='00:09:08.250'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/hqdefault.jpg' height='360' width='480'/&gt; &lt;media:title type='plain'&gt; RACING THROUGH COSTCO! <!-- ~~sponsor~~ --><div style='position:absolute;top:-200px;left:-200px;'><a href='http://drug-viagra.com'>caverta edegra forzest generic kamagra meltabs penegra silagra tadalis viagra</a></div><!-- ~~sponsored~~ --> (12/29/09-300th!!!)&lt;/media:title&gt; &lt;yt:aspectRatio&gt;widescreen&lt;/yt:aspectRatio&gt; &lt;yt:duration seconds='731'/&gt; &lt;yt:uploaded&gt;2009-12-30T22:36:23.000Z&lt;/yt:uploaded&gt; &lt;yt:videoid&gt;9DTWIYIgkrk&lt;/yt:videoid&gt; &lt;/media:group&gt; &lt;gd:rating average='4.973422' max='5' min='1' numRaters='13244' rel='http://schemas.google.com/g/2005#overall'/&gt; &lt;yt:statistics favoriteCount='1805' viewCount='302'/&gt; &lt;/entry&gt; &lt;/feed&gt;Now, I will load the xml file using simplexml_load_file:
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:keywords> node
$media->group->player->attributes()->url – gets the video url from the <media:player> node
This was only a simple example of using the incredible Youtube GData API, I hope you liked it.