3

i've found that I can get a list of a youtube channel's videos by viewCount (hits) and limit the results with the link below

http://gdata.youtube.com/feeds/api/users/edbassmaster/uploads?orderby=viewCount&max-results=5

this of course returns some xml feed code. I am trying to list the videos onto my website in a div.

what i've tried:

<?php
    $list = Array(); 
    //get the xml data from youtube 
    $url = "http://gdata.youtube.com/feeds/api/users/edbassmaster/uploads?orderby=viewCount&max-results=5"; 
    $xml = simplexml_load_file($url); 
    //load up the array $list['title'] = $xml->title[0]; 
    $list['title'] = $xml->title[0]; 
    echo $list['title'];
?>

So far that just gives me the title for the xml feed, if I try $xml->title[1]. It doesn't return anything. How can I use that xml feed to list the titles and (href) link them to the videos? I'm trying to retrieve 2 things from the xml source, the title of the video and the url.

Thanks.

Tobias Golbs
  • 4,586
  • 3
  • 28
  • 49
Guage
  • 85
  • 10
  • 1
    Can you try to make a `var_dump($xml)` and give the result? I think the problem is that what you are looking for is a child element in fact. – MisterJ Jul 30 '13 at 11:53

2 Answers2

3

Something like this:

 <?php
$url = "http://gdata.youtube.com/feeds/api/users/edbassmaster/uploads?orderby=viewCount&max-results=5";
$xml = simplexml_load_file($url);
foreach($xml->entry as $entry){
    echo "Title: ".$entry->title."<br />";
    echo "Link :".$entry->link['href']."<br />";
}
?>
AxelPAL
  • 1,047
  • 3
  • 12
  • 19
  • This helped me out with my own question (http://stackoverflow.com/questions/22880348/cant-access-xml-node-via-xpath-yt-channel-feed). I'm curious why `$xml->entry` works but `$xml->xpath('entry')` seemingly doesn't... (returns 0 nodes). – Mitya Apr 05 '14 at 11:50
0

The first title tag under the doc root constains the title of feed, which there is only one. To access the subsequent entries, use $xml->entry[0]

<?php
  $list = Array();
      // get the xml data from youtube 
      $url = "http://gdata.youtube.com/feeds/api/users/edbassmaster/uploads?orderby=viewCount&max-results=5";
      $xml = simplexml_load_file($url);
      // load up the array $list['entry'] = $xml->title[0]; 
      $entries = $xml->entry;
//echo preg_replace("feed","fd"file_get_contents($url)); 
  echo $entries[1]->title;//entry[0],entry[1],entry[2]... will work
         ?>
SoWhat
  • 5,564
  • 2
  • 28
  • 59