Php 从youtube频道获取最新视频
我想把我频道的最新视频放在我的网站上。这将返回以下错误:Php 从youtube频道获取最新视频,php,youtube,Php,Youtube,我想把我频道的最新视频放在我的网站上。这将返回以下错误: 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
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>';
}
?>
你实际上应该做一个卷发
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调用,请查看->
api的参考在这里->
您甚至可以在交互式编辑器中进行尝试。它指出存在403错误,不允许您以尝试的方式访问URL