2015-10-15 88 views
0

因此,我的网页设法通过Youtube API连接YouTube频道,并保存令牌等。但我没有找到任何代码示例来检索YouTube的个人资料图片。如何检索YouTube频道(PHP)的个人资料图片?

我在去年发现了另一个线程,其中有人用json脚本回答。它看起来如下:

var json = new WebClient().DownloadString("http://gdata.youtube.com/feeds/api/users/"+YT_CHANNEL_NAME); 
    string str = "thumbnail url='"; 
    string ImagePic = json.Substring(json.IndexOf("thumbnail url") + str.Length); 
    if (ImagePic.Contains("'")) 
    { 
     ImagePic = ImagePic.Remove(ImagePic.IndexOf("'")); 
     ImageChannel.ImageUrl = ImagePic; 

    } 

我没有在任何JSON的经验和无法弄清楚,如果这仍然是我的一个合适的代码,以及如何实现它在我的PHP代码。

+0

根据文档,您可以访问个人资料图片:https://developers.google.com/youtube/2.0/ developers_guide_protocol_profiles?hl = en – DataHerder

+0

其次,这基本上是服务器端JavaScript,Node.js。你不能直接翻译它,它是与他们自己的实现太不同的脚本语言。这就是说,你看过php-youtube api框架吗?您需要下载该文件,并在此处查看获取用户的答案:http://stackoverflow.com/questions/22117243/getting-user-info-google-php-client-issue – DataHerder

回答

1

这是如何从PHP中的YouTube频道获取个人资料图片。

1,下载PHP API here

2,让您的API key

4,你还需要渠道ID,如果这是你自己的账号,你可以从here

得到它这是php代码

<?php 

$DEVELOPER_KEY = 'YOUR_KEY'; 

// Call set_include_path() as needed to point to your client library. 
require_once('/path/to/api/src/Google_Client.php'); 
require_once('/path/to/api/src/contrib/Google_YouTubeService.php'); 

$client = new Google_Client(); 
$client->setDeveloperKey($DEVELOPER_KEY); 

$youtube = new Google_YoutubeService($client); 
$htmlBody = ''; 
try { 

    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
     'id' => 'CHANNEL_ID', 
     'part' => 'snippet', 
    )); 


    //echo '<pre>'; 
    //print_r($channelsResponse); 
    //echo '</pre>'; 
    $img = $channelsResponse['items'][0]['snippet']['thumbnails']['default']['url']; 

    echo "<img src='$img'>"; 

} catch (Google_ServiceException $e) { 
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); 
} catch (Google_Exception $e) { 
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); 
} 
echo $htmlBody; 
?> 

它作为常规数组返回并且文档是here,您也可以使用'中等'或'高'代替'默认'

+1

是的,而新的YouTube API有点痛苦。超级健谈。 – MichaelClark

相关问题