2016-03-05 212 views
3

我並不完全知道如何標題,但是我創建了一個API來檢索關於某人的Youtube頻道的某個JSON日期,並且它工作正常,並且直到我遇到一個這樣的頻道, https://www.youtube.com/channel/UCRMZyEp0IzcI0IGpDFRh6fQ獲取沒有顯示名稱的YouTube頻道ID

他的賬號是在url中而不是 https://www.youtube.com/user/iceycat25

當我運行API並搜索「iceycat25」時,數據被正常拖拉,但是如果我搜索「MOTO TIME」,則API不會返回此類用戶。有沒有什麼辦法解決這一問題? 下面是我現在的相關代碼。

// obtain your own API key at 
// https://console.developers.google.com 
$MyKey = xxxxxxxxxx_xxxxxxxxxx_xxxxxxxxxx; 
// Convert the GET data into variables. 
$YoutubeName = $_GET["Channel_Name"]; 
$Command = $_GET["Command"]; 

// Get the channel ID and Upload ID from the YouTube name inputted 
$IDs = json_decode(@file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=$YoutubeName&key=$MyKey"), true); 
if($IDs['items'][0]['id'] == null) 
{ 
    echo 'No such YouTube channel exists.'; 
    die(); 
} 

回答

0

我已經在我的論壇中實現了它作爲div。 正如您在這裏看到的YouTube API並且不接受相同參數中的用戶名和ID。

隨着ID:

<div class="g-ytsubscribe" data-***channelid***="CHANNELID" data-layout="full" data-count="default"></div> 

使用用戶名:

<div class="g-ytsubscribe" data-***channel***="{$user.customFields.youtube_us}" data-layout="full" data-count="default"></div> 

你必須改變的屬性。

只要試一試https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=$YoutubeName&key=$MyKey,它應該工作,如果沒有 - 發表評論。

更多:https://developers.google.com/youtube/v3/docs/channels/list#examples

+0

是的,似乎是這樣。 https://developers.google.com/youtube/v3/guides/working_with_channel_ids – Alpix

+0

如果我只是使用'id = $ YoutubeName'而不是'forUsername = $ YoutubeName',當我搜索「iceycat25」或「MOTO TIME」 。這要求我事先知道頻道ID。回顧最終用戶提供他們自己的渠道ID的情況會更好,因爲顯示名稱不是唯一的,他們可能會爲錯誤的用戶提取數據。我只是把這個接受了。感謝您的幫助! –

相關問題