用php显示YouTube的私人视频

  • 本文关键字:视频 YouTube php 显示 php
  • 更新时间 :
  • 英文 :


我正在尝试在我的php项目中显示我在youtube上上传的私人视频。以下是我的php代码,

<?php
//Get videos from channel by YouTube Data API
$API_key    = 'YOUR_YOUTUBE_API_KEY';
$channelID  = 'YOUR_YOUTUBE_CHANNEL_ID';
$maxResults = 10;
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Videos from YouTube Channel </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">
.container{padding: 15px;}
.youtube-video h2{font-size: 16px;}
</style>
</head>
<body>
<div class="container">
<div class="row">
<?php
foreach($videoList->items as $item){
//Embed video
if(isset($item->id->videoId)){
echo '<div id="'. $item->id->videoId .'" class="col-lg-3 col-sm-6 col-xs-6 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>';
}
}
?>
</div>
</div>
</body>
</html>

但是我得到以下错误,

Warning: file_get_contents(https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=UC40vLh6NqMw_F-QINO-keJg&maxResults=10&key=AIzaSyDLrY): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in C:xampphtdocschannelsindex.php on line 9
Notice: Trying to get property 'items' of non-object in C:xampphtdocschannelsindex.php on line 31

警告:在第 31 行的 C:\xampp\htdocs\channels\index.php 中为 foreach(( 提供的参数无效

你能帮我解决吗

您的代码可能没有任何问题。

您只是使用该 api 密钥超出了该帐户的使用限制

最新更新