【问题标题】:How to retrieve the profile picture of a youtube channel (PHP)?如何检索 youtube 频道 (PHP) 的个人资料图片?
【发布时间】:2016-01-14 13:35:16
【问题描述】:

所以我的页面设法通过 Youtube API 连接 youtube 频道并保存令牌等。但我只是没有找到任何代码示例来检索 youtube 个人资料图片。

我发现去年的另一个帖子,有人用 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 代码中实现它。

【问题讨论】:

  • 根据文档,您确实可以访问个人资料图片:developers.google.com/youtube/2.0/…
  • 其次这是服务器端 JavaScript,基本上是 Node.js。您不能直接翻译它,它具有自己的实现的脚本语言太不同了。话虽如此,您是否研究过 php-youtube api 框架?您需要下载它,还可以在此处查看答案以获得用户:stackoverflow.com/questions/22117243/…

标签: php json image youtube channel


【解决方案1】:

这是在 PHP 中从 youtube 频道获取个人资料图片的方法。

1、下载php apihere

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,您也可以使用“medium”或“high”而不是“default”

【讨论】:

  • 是的,新的 YouTube API 有点麻烦。超级健谈。
猜你喜欢
  • 2020-08-15
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多