如何检索YouTube频道的个人资料图片(PHP)?

agxfikkp  于 2023-04-28  发布在  PHP
关注(0)|答案(2)|浏览(284)

因此,我的页面通过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代码中实现它。

iaqfqrcu

iaqfqrcu1#

这是如何在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,您也可以使用'medium'或'high'代替'default'

xqnpmsa8

xqnpmsa82#

<?php
$channel_name = "DrIsrarAhmed_Official"; // replace with the name of the YouTube channel you want to fetch the profile picture of
$channel_id = ""; // initialize an empty variable to store the channel ID
$profile_picture_url = ""; // initialize an empty variable to store the profile picture URL

// Step 1: Get the channel ID by channel name
$api_key = "YOUR_KEY_HERE"; // replace with your YouTube Data API v3 API key
$url = "https://www.googleapis.com/youtube/v3/search?part=snippet&type=channel&q=" . urlencode($channel_name) . "&key=" . $api_key;
$response = file_get_contents($url);

if ($response) {
  $data = json_decode($response, true);
  $channel_id = $data['items'][0]['snippet']['channelId'];
} else {
  echo "Error fetching data";
}

// Step 2: Get the profile picture URL by channel ID
if ($channel_id) {
  $url = "https://www.googleapis.com/youtube/v3/channels?part=snippet&id=" . $channel_id . "&key=" . $api_key;
  $response = file_get_contents($url);
  
  if ($response) {
    $data = json_decode($response, true);
    $profile_picture_url = $data['items'][0]['snippet']['thumbnails']['high']['url'];
  } else {
    echo "Error fetching data";
  }
}

if ($profile_picture_url) {
  echo "<img src=\"$profile_picture_url\" alt=\"$channel_name's profile picture\">";
} else {
  echo "Error fetching profile picture";
}
?>

相关问题