Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display xml output from youtube api - php

Tags:

php

youtube

xml

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.

like image 390
Guage Avatar asked May 21 '26 06:05

Guage


1 Answers

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 />";
}
?>
like image 199
AxelPAL Avatar answered May 24 '26 09:05

AxelPAL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!