PHP:如何解析从 YouTube JSON 响应派生的关联数组?
我想做的是从 YouTube 频道获取每个视频的视频信息,然后将其打印在页面上,只有 15 个视频,因此应该完全在使用限制之内。
我已经阅读了网站上的一些教程和许多 SO 问题,但我似乎仍然无法将我需要的内容放在一起:/
以下是我迄今为止的尝试:
$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'];
其基础来自: php youtube json 解码
我也看过使用 PHP 循环代码
我理想的输出是这样的:
Video Title
Video Description
Rating
Anything else useful that is returned in the json response.
Embedded video
我知道这可能是可能的获得视频的直接链接,但在阅读完此内容后,我确信如果我能做到这一点,我将能够将其嵌入。
我还将网址中的 json
更改为“jsonc”,每个返回的内容似乎有很大差异,我更改的原因是因为我确信 YouTube 上它说要使用 jsonc ?
非常感谢任何帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您在 http://pastebin.com/eZ6U3RmS 上的粘贴,正确的代码是:
According to your paste on http://pastebin.com/eZ6U3RmS the right code would be:
请求:http://gdata.youtube.com/feeds/api/videos/[VIDEO_ID_HERE] 将为您提供包含所有视频数据的 xml 响应,也许这会对您有所帮助
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