我正在使用以下代码显示我网站上用户的所有视频:
<?php
$xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/XXXXXXX/uploads');
$server_time = $xml->updated;
$return = array();
foreach ($xml->entry as $video) {
$vid = array();
$vid['id'] = substr($video->id,42);
$vid['title'] = $video->title;
array_push($return, $vid);
}
?>
<h2>Video Gallery</h2>
<?php
foreach($return as $video) {
?>
<div class="col-md-4">
<div style="height: auto;" class="featured-box featured-box-secundary">
<div class="box-content clearfix">
<h4><?= $video['title'] ?></h4>
<iframe width="270" height="203" src="https://www.youtube.com/embed/<?=$video['id']?>?rel=0&showinfo=0" allowfullscreen></iframe>
</div></div></div>
<?php
}
?>
但是我收到了不推荐使用的通知。如何使用API v3将$ vid ['id']和$ vid ['title']放入HTML?
最佳答案
是的,Youtube API V2已死。
要向Youtube API V3发出请求,您需要进行身份验证。在获取{YOUR_API_KEY}
Google Developers Console。您需要创建new project并启用Youtube API。
之后,您可以发出以下命令:
第一channels#list
方法将返回带有有关 channel 的一些信息的JSON
,包括“上传”播放列表的{PLAYLIST_ID}
:
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={USERNAME}&key={YOUR_API_KEY}
第二
使用
{PLAYLIST_ID}
,您可以使用playlistItems#list
方法获取用户上传的视频:https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={PLAYLIST_ID}&key={YOUR_API_KEY}
您可以在以下位置测试以上代码:
Youtube apis-explorer
关于php - Youtube在API v3 PHP中获取用户的视频ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29827873/