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!

5
  • to be honest Rufinius I'm not too sure! I got that bit of code from one of the other questions I was viewing. Commented Dec 13, 2011 at 12:57
  • Hi Rufinus, sorry for the delay in replying, I was given another task in work yesterday. $body1 contains the json response from YouTube from the looks of things. Commented Dec 14, 2011 at 9:13
  • try to print_r(json_decode($body1)); im not sure this str_replace you have in there is needed. Commented Dec 14, 2011 at 11:59
  • yeah, I've changed the code slightly now. Commented Dec 14, 2011 at 12:03
  • kann you poste the output of the print_r Commented Dec 14, 2011 at 12:14

2 Answers 2

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....
}
Sign up to request clarification or add additional context in comments.

1 Comment

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

4 Comments

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.
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
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 sorry.. i am not able to provide anything more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.