PHP:如何解析从 YouTube JSON 响应派生的关联数组?

发布于 2024-12-20 18:13:18 字数 1260 浏览 1 评论 0原文

我想做的是从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下伊人醉 2024-12-27 18:13:18

根据您在 http://pastebin.com/eZ6U3RmS 上的粘贴,正确的代码是:

foreach($data['data']['items'] as $item) {
    echo $item['title'];
    echo $item['description'];
    echo $item['rating'];
    echo $item['player']['default'];
    // whatever you need....
}

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....
}
橘味果▽酱 2024-12-27 18:13:18

请求: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文