2015-10-15 57 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

相關問題