Php tips, wordpress hacks

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:

http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?time=today&start-index=1&max-results=5

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:

<?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'/>
    &amp;lt;title&amp;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!!!)&amp;lt;/title&amp;gt;
    &amp;lt;content type='application/x-shockwave-flash' src='http://www.youtube.com/v/9DTWIYIgkrk?f=standard&amp;amp;c=ytapi-google-jsdemo&amp;amp;app=youtube_gdata'/&amp;gt;
    &amp;lt;link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=9DTWIYIgkrk&amp;amp;feature=youtube_gdata'/&amp;gt;
    &amp;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'/&amp;gt;
    &amp;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'/&amp;gt;
    &amp;lt;link rel='http://gdata.youtube.com/schemas/2007#mobile' type='text/html' href='http://m.youtube.com/details?v=9DTWIYIgkrk'/&amp;gt;
    &amp;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'/&amp;gt;
    &amp;lt;author&amp;gt;
      &amp;lt;name&amp;gt;SHAYTARDS&amp;lt;/name&amp;gt;
      &amp;lt;uri&amp;gt;http://gdata.youtube.com/feeds/api/users/shaytards&amp;lt;/uri&amp;gt;
    &amp;lt;/author&amp;gt;
    &amp;lt;gd:comments&amp;gt;
      &amp;lt;gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/9DTWIYIgkrk/comments?client=ytapi-google-jsdemo' countHint='2837'/&amp;gt;
    &amp;lt;/gd:comments&amp;gt;
    &amp;lt;media:group&amp;gt;
      &amp;lt;media:category label='Entertainment' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'&amp;gt;Entertainment&amp;lt;/media:category&amp;gt;
      &amp;lt;media:content url='http://www.youtube.com/v/9DTWIYIgkrk?f=standard&amp;amp;c=ytapi-google-jsdemo&amp;amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='731' yt:format='5'/&amp;gt;
      &amp;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'/&amp;gt;
      &amp;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'/&amp;gt;
      &amp;lt;media:credit role='uploader' scheme='urn:youtube' yt:type='partner'&amp;gt;SHAYTARDS&amp;lt;/media:credit&amp;gt;
      &amp;lt;media:description type='plain'&amp;gt;My Twitter http://www.twitter.com/shaycarl

My Dailybooth http://www.dailybooth.com/shaycarl&amp;lt;/media:description&amp;gt;
      &amp;lt;media:keywords&amp;gt;shaycarl, shaytards, costco, love, the, smell, of, new, balls&amp;lt;/media:keywords&amp;gt;
      &amp;lt;media:player url='http://www.youtube.com/watch?v=9DTWIYIgkrk&amp;amp;feature=youtube_gdata'/&amp;gt;
      &amp;lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/default.jpg' height='90' width='120' time='00:06:05.500'/&amp;gt;
      &amp;lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/2.jpg' height='90' width='120' time='00:06:05.500'/&amp;gt;
      &amp;lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/1.jpg' height='90' width='120' time='00:03:02.750'/&amp;gt;
      &amp;lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/3.jpg' height='90' width='120' time='00:09:08.250'/&amp;gt;
      &amp;lt;media:thumbnail url='http://i.ytimg.com/vi/9DTWIYIgkrk/hqdefault.jpg' height='360' width='480'/&amp;gt;
      &amp;lt;media:title type='plain'&amp;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!!!)&amp;lt;/media:title&amp;gt;
      &amp;lt;yt:aspectRatio&amp;gt;widescreen&amp;lt;/yt:aspectRatio&amp;gt;
      &amp;lt;yt:duration seconds='731'/&amp;gt;
      &amp;lt;yt:uploaded&amp;gt;2009-12-30T22:36:23.000Z&amp;lt;/yt:uploaded&amp;gt;
      &amp;lt;yt:videoid&amp;gt;9DTWIYIgkrk&amp;lt;/yt:videoid&amp;gt;
    &amp;lt;/media:group&amp;gt;
    &amp;lt;gd:rating average='4.973422' max='5' min='1' numRaters='13244' rel='http://schemas.google.com/g/2005#overall'/&amp;gt;
    &amp;lt;yt:statistics favoriteCount='1805' viewCount='302'/&amp;gt;
  &amp;lt;/entry&amp;gt;
&amp;lt;/feed&amp;gt;

Now, I will load the xml file using simplexml_load_file:

$feed = &quot;http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=1&amp;max-results=1&amp;time=today&quot;;
$xml = simplexml_load_file($feed);
echo '&lt;pre&gt;';
print_r($xml);
echo '&lt;pre&gt;';

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 = &quot;http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?start-index=2&amp;max-results=1&amp;time=today&quot;;
// load xml
$xml = simplexml_load_file($feed);
// parse every node &lt;entry&gt; to get the video information
foreach ($xml-&gt;entry as $video) {
	echo '&lt;h2&gt;'.$video-&gt;title.'&lt;/h2&gt;&lt;br&gt;'; // video title
	echo '&lt;pre&gt;'.$video-&gt;content.'&lt;/pre&gt;&lt;br&gt;'; // video content
	// media: namespace
    $media = $video-&gt;children('http://search.yahoo.com/mrss/');
	echo 'Tags: '.$media-&gt;group-&gt;keywords.'&lt;br&gt;'; // video tags
	echo 'Video url: '.$media-&gt;group-&gt;player-&gt;attributes()-&gt;url.'&lt;br&gt;'; // video url
	echo 'Uploaded by '.$video-&gt;author-&gt;name.'&lt;br&gt;'; // author name
	echo '&lt;hr&gt;';
}

$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.

<media:keywords

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>