2015-04-23 100 views
2

我使用这个代码来显示用户的所有影片在我的网站:的Youtube获取用户的视频的ID在API V3 PHP

<?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&amp;showinfo=0" allowfullscreen></iframe> 
     </div></div></div> 
    <?php 
    } 
?> 

但我收到了已过时的通知。我可以如何使用API​​ v3将$ vid ['id']和$ vid ['title']放入HTML中?

+0

请告诉我的'$返回输出'?你得到了哪些不赞成的通知? – Alex

+0

该通知是youtube与info中的额外视频。 –

回答

0

是的,Youtube API V2已经死了。

要向Youtube API V3发出请求,您需要进行身份验证。获取{YOUR_API_KEY}Google Developers Console。您需要创建new project并启用Youtube API。

之后,你就可以发出以下命令:

首先

channels#list方法将返回一个JSON与该频道的一些信息,包括{PLAYLIST_ID}的“上传”播放列表:

https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={USERNAME}&key={YOUR_API_KEY} 

与该playlistItems#list方法{PLAYLIST_ID}你可以得到用户上传的视频:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId={PLAYLIST_ID}&key={YOUR_API_KEY} 

您可以测试在上面的代码:

Youtube apis-explorer

+0

为什么我需要进行身份验证才能获取用户视频列表? –

+0

因为在Youtube API V3谷歌决定如此... :( –

+0

好的,API密钥必须是上传视频的用户? –

相关问题