Getting Youtube videos using json and php
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=vie开发者_StackOverflowwCount&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'];
}
?>
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 />';
}
精彩评论