3

I'm trying to get a video url and title from the Youtube API. Everything is working fine, but I can't show the data - I want to show url and title of the video using json and php only.

My code:

 <?php
$get = file_get_contents("http://gdata.youtube.com/feeds/api/videos?vq=cod&orderby=viewCount&max-results=1&start-index=1&alt=json");
$decode = json_decode($get, TRUE); // TRUE for in array format

foreach($decode as $res) { 

echo $res['title']['$t'];
}
?>
2
  • You only asked your question three minutes ago - it can sometimes take half an hour, an hour, a day, or longer for somebody to answer your question. Please be patient. Commented Sep 9, 2011 at 20:23
  • ok, my question is how to get the data ? this query isnt working. i think im not using the correct tags ( $res['title']['$t'] ) ,im totally confused ! visit this url to figure it out : gdata.youtube.com/feeds/api/… Commented Sep 9, 2011 at 20:25

1 Answer 1

6

For this example, you don't really need the foreach loop because there's only one returned result, but for multiple results, you'll want to do something like the following...

foreach ($decode['feed']['entry'] as $entry) {
    echo '<a href="' . $entry['link'][0]['href'] . '">' . $entry['title']['$t'] . '</a><br />';
}
Sign up to request clarification or add additional context in comments.

Comments

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.