【问题标题】:Get latest video from youtube channel从 youtube 频道获取最新视频
【发布时间】:2020-08-17 22:45:44
【问题描述】:

我想将我频道中的最新视频放到我的网站上。这将返回以下错误:

Warning: file_get_contents(https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=UCD---------&maxResults=1&key=A---------------): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in C:\xampp\htdocs\inc\latestVideo.php on line 15

我的代码:

<?php

$API_key    = 'A---------------';
$channelID  = 'UCD---------';
$maxResults = 1;

$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

    if(isset($item->id->videoId)){
        echo '<div class="youtube-video">
                <iframe width="280" height="150" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
                <h2>'. $item->snippet->title .'</h2>
            </div>';
    }

?>

【问题讨论】:

  • 它指出有一个403错误,您不能以您尝试的方式访问该URL

标签: php youtube


【解决方案1】:

你实际上应该做一个卷曲

curl \
    'https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC_x5XG1OV2P6uZZ5FSM9Ttw&maxResults=1&key=[YOUR_API_KEY]' \
     --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
     --header 'Accept: application/json' \
     --compressed

请注意,除了 API 密钥之外,标头上还需要授权不记名令牌。 channelId 也需要是 id。

要了解如何从 PHP 进行 curl 调用,请查看 -> php curl: I need a simple post request and retrival of page example

api 参考在这里 -> https://developers.google.com/youtube/v3/code_samples/code_snippets?apix_params=%7B%22part%22%3A%22snippet%2CcontentDetails%2Cstatistics%22%2C%22id%22%3A%22UC_x5XG1OV2P6uZZ5FSM9Ttw%22%7D&apix=true

您甚至可以在交互式编辑器中尝试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2020-04-10
    • 2019-08-10
    • 2012-10-05
    • 1970-01-01
    • 2019-10-06
    • 2012-10-24
    相关资源
    最近更新 更多