2015-04-23 102 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密鑰必須是上傳視頻的用戶? –

相關問題