Powered By

Load Youtube toprated videos using simplexml_load_file PHP

Tags : | | |
  In this post I will show you how to fetch the you tube top rated videos just with built in PHP functions instead of using zend data api libraries.  Zend data api is the best and optimized way to do this task but rather than including and working with zend and zend data api you can just do this with php built-in functions by reading the you tube feeds for the top rated videos.

 So here we go.
General feed url of youtube is

http://gdata.youtube.com/feeds/api/standardfeeds/FEED_IDENTIFIER 
 
Youtube feeds URL for the top rated videos is
http://gdata.youtube.com/feeds/api/standardfeeds/top_rated

Here you can replace top_rated with




Fore more info regarding this

http://code.google.com/apis/youtube/2.0/developers_guide_php.html

So we have to read the feeds from the above url, then you can display it where ever you want.

We have the top rated videos as xml so we need to parse it using php function "simplexml_load_file() " .
To do this in php.ini we need to enable the url include option to On if it is not already turned on.

Get the feeds in a variable

                     $url="http://gdata.youtube.com/feeds/api/standardfeeds/top_rated";
                     $comments =simplexml_load_file($url); // Php built in function


So we have  top rated videos in $comments  , we need to parse it using foreach and display it in the following way.

 Here the feed is in yahoo feed format so you to mention the feed as yahoo and then you can parse the media group under entry tag.

      $media = $video->children('http://search.yahoo.com/mrss/');
So from this we can easily parse all the data from the youtube feeds.

Download the source for your reference.





You can also do all other functionality like searching a video or displaying particular video comments, recent videos and more using the above method. Its very easier than the zend data api. Try it out. Leave the comments if you have any problem

Related Posts by Categories