Guage
Guage

Reputation: 85

display xml output from youtube api - php

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.

Upvotes: 3

Views: 1940

Answers (2)

SoWhat
SoWhat

Reputation: 5622

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
         ?>

Upvotes: 0

AxelPAL
AxelPAL

Reputation: 1027

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 />";
}
?>

Upvotes: 3

Related Questions