Jump to content

Working with the YouTube API


siobhyb

Recommended Posts

Hi,

 

I'm working with the YouTube API to echo a feed based on a user's query.

 

My current code is below.  With this code, a whole list of videos is retrieved.

 

However, in order to present the feed as I wish to... I need to return each video in the feed individually. 

 

I am unsure how to do this and any advice would be really appreciated.

 

 $feedURL = "http://gdata.youtube.com/feeds/api/videos/-/$youtubeQuery?orderby=viewCount&max-results=10";
                
          // read feed into SimpleXML object
          $sxml = simplexml_load_file($feedURL);

          // iterate over entries in resultset
          // print each entry's details

          foreach ($sxml->entry as $entry) {
          // get nodes in media: namespace for media information
          $media = $entry->children('http://search.yahoo.com/mrss/');
                  
          // get video player URL
          $attrs = $media->group->player->attributes();
          $watch = $attrs['url']; 
                  
          // get video thumbnail
          $attrs = $media->group->thumbnail[0]->attributes();
          $thumbnail = $attrs['url'];
                  
          // get <yt:duration> node for video length
          $yt = $media->children('http://gdata.youtube.com/schemas/2007');
          $attrs = $yt->duration->attributes();
          $length = $attrs['seconds'];

          // get video title
          $title = $media->group->title;
                 
          // get <gd:rating> node for video ratings
          $gd = $entry->children('http://schemas.google.com/g/2005'); 
          if ($gd->rating) {
          $attrs = $gd->rating->attributes();
          $rating = $attrs['average'];
          } else {
          $rating = 0;
          }
            
          }
          ?>

 

Link to comment
Share on other sites

Im not sure about the output from the feedURL variable (and I'm on my iPhone now so I can't test it) but if you could use

 echo "<pre>".file_get_contents($feedURL)."</pre>"

That would at least tell you what you're dealing with. 

You may have nested arrays which you can then deal with easily.

Link to comment
Share on other sites

Hi,

 

I'm still a bit stuck with this.

 

I've got the original code...

 

    $feedURL = "http://gdata.youtube.com/feeds/api/videos/-/royalwedding?orderby=viewCount&max-results=10";
                
          // read feed into SimpleXML object
          $sxml = simplexml_load_file($feedURL);

          // iterate over entries in resultset
          // print each entry's details
          foreach ($sxml->entry as $entry) {
          // get nodes in media: namespace for media information
          $media = $entry->children('http://search.yahoo.com/mrss/');
                  
          // get video player URL
          $attrs = $media->group->player->attributes();
          $watch = $attrs['url']; 
                  
          // get video thumbnail
          $attrs = $media->group->thumbnail[0]->attributes();
          $thumbnail = $attrs['url'];
                  
          // get <yt:duration> node for video length
          $yt = $media->children('http://gdata.youtube.com/schemas/2007');
          $attrs = $yt->duration->attributes();
          $length = $attrs['seconds'];

          // get video title
          $title = $media->group->title;
                 
          // get <gd:rating> node for video ratings
          $gd = $entry->children('http://schemas.google.com/g/2005'); 
          if ($gd->rating) {
          $attrs = $gd->rating->attributes();
          $rating = $attrs['average'];
          } else {
          $rating = 0;
          }

 

If I echo the following code...

 

 // print record

echo "<div class=fvid><a href=\"{$watch}\"><img src=\"$thumbnail\"/></a></div>";

echo "<ul class='vidselector'><li><a href=\"{$watch}\">{$media->group->title}</span></a></li></ul>";

 

Then the last video in the feed in printed out.

 

If I put the same code in the "For" loop then all the videos in the feed are printed out. 

 

I want to find a way of choosing which videos in the feed I want to display.  For example, maybe just the third, fourth and sixth.

 

I don't know how to begin doing this.  Any advice would be really appreciated.  Thank you.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.