0

What I am trying to do is fetch the video information for each video from a YouTube channel and just print them out on a page, there are only 15 videos so it should be well within the usage limits.

I have read a few tutorials on websites and many SO questions but I still can't seem to put together what I need :/

The following is my attempt so far:

$url = 'http://gdata.youtube.com/feeds/api/videos?max-results=20&alt=jsonc&orderby=published&format=5&safeSearch=none&author=OpticalExpressUK&v=2';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body = curl_exec($ch);
curl_close($ch);
$data = json_decode($body, true);

echo '<pre>'; 
var_dump($data);
echo '</pre>'; 

// trying to get something out of the array!
echo $data[1]['title'];

The foundations of this came from: php youtube json decode

I've also looked at Looping code using PHP

My ideal output would be something along the lines of:

Video Title

Video Description

Rating

Anything else useful that is returned in the json response.

Embedded video

I know it may only be possible to get the direct link for the video but after reading about this I'm sure I would be able to get it embedded if I could get it that far.

I've also changed the json in the url to 'jsonc', there seems to be a big difference between what each returns, reason I changed was because I'm sure on YouTube it says to use jsonc?

Any help is much appreciated!

Community
  • 1
  • 1
martincarlin87
  • 10,848
  • 24
  • 98
  • 145

2 Answers2

1

According to your paste on http://pastebin.com/eZ6U3RmS the right code would be:

foreach($data['data']['items'] as $item) {
    echo $item['title'];
    echo $item['description'];
    echo $item['rating'];
    echo $item['player']['default'];
    // whatever you need....
}
Rufinus
  • 29,200
  • 6
  • 68
  • 84
  • ah, superb. thanks very much for your patience throughout this question, it's not easy to keep track of answering someone's insane questions over a long period of time, thank you! – martincarlin87 Dec 15 '11 at 09:06
0

a request to : http://gdata.youtube.com/feeds/api/videos/[VIDEO_ID_HERE] will give you an xml response with all the video data Perhaps this will help you

andrew
  • 2,058
  • 2
  • 25
  • 33
  • Hi Andrew, thanks but I was trying to do it all programmatically only knowing the channel and getting all the video information using that and then if need be, the feed you provided the link for each video aswell. I might have to do it this way though. – martincarlin87 Dec 13 '11 at 12:56
  • i havent used your link at all, so i may say something stupid right now... but what if you compbine the 2 links? one to get video list and the second to get video data? i dont know what your link provides, so once again, i might say somethin stupd – andrew Dec 13 '11 at 14:48
  • Hi andrew, not sure if that can be done, I think I would need to get the links for each video from the channel feed and store them, then go through each one and do what you suggested. – martincarlin87 Dec 14 '11 at 09:11
  • @martincarlin87 sorry.. i am not able to provide anything more – andrew Dec 14 '11 at 10:57