2016-03-05 213 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的情况会更好,因为显示名称不是唯一的,他们可能会为错误的用户提取数据。我只是把这个接受了。感谢您的帮助! –

相关问题