我正试图从Youtube API获取视频网址和标题.一切都工作正常,但我无法显示数据 - 我想只使用json和php显示视频的网址和标题.
我的代码:
<?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'];
}
?>
Run Code Online (Sandbox Code Playgroud)
对于这个例子,你真的不需要foreach循环,因为只有一个返回的结果,但是对于多个结果,你会想要做类似下面的事情......
foreach ($decode['feed']['entry'] as $entry) {
echo '<a href="' . $entry['link'][0]['href'] . '">' . $entry['title']['$t'] . '</a><br />';
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5112 次 |
最近记录: |