PHP - 我们如何通过 Youtube API 获取 Youtube 观看次数和评论以及视频列表?

PHP - How do we can get Youtube Views and comments count along with videos list via Youtube APIs?

我正在从 youtube API 获取视频列表。我还想从 youtube apis.

获得观看次数和评论数作为回应

我正在尝试以下方法。

$client = new \Google_Client();
$client->setApplicationName('API code samples');
$client->setDeveloperKey('api_key');
echo "<pre>";
$queryParams = [
    'channelId' => 'channel_id',
    'maxResults' => 5,
    'order' => 'date',
    'type' => 'video'
];

$response = $service->search->listSearch('snippet', $queryParams);
print_r($response);

Youtube API 回应

[snippet] => Google\Service\YouTube\SearchResultSnippet Object
    (
        [channelId] => Channel_id
        [channelTitle] => João Patrício
        [description] => Mohamed Said made an amazing video demonstrating how to easily develop a Nuxt and Laravel API application, and now let's ...
        [liveBroadcastContent] => none
        [publishedAt] => 2021-12-08T08:57:21Z
        [thumbnailsType:protected] => Google\Service\YouTube\ThumbnailDetails
        [thumbnailsDataType:protected] => 
        [title] => Deploying LaravelAPI and NuxtJs App using Traefik and Docker - 2nd Episode

我正在获取视频标题、描述等 是否可以在同一个 api 响应中获得视频的观看次数和评论数?

请帮帮我。谢谢

我正在获取视频标题、描述等是否可以在同一 api 响应中获取视频的观看次数和评论数?

不,这是不可能的。

Search.list方法

您正在使用 search.list method Which returns a list of search#resource

{
  "kind": "youtube#searchResult",
  "etag": etag,
  "id": {
    "kind": string,
    "videoId": string,
    "channelId": string,
    "playlistId": string
  },
  "snippet": {
    "publishedAt": datetime,
    "channelId": string,
    "title": string,
    "description": string,
    "thumbnails": {
      (key): {
        "url": string,
        "width": unsigned integer,
        "height": unsigned integer
      }
    },
    "channelTitle": string,
    "liveBroadcastContent": string
  }
}

Video.list方法

要恢复评分,您需要使用 videos.list which will return a video resource 其中确实包含您正在寻找的统计数据。

  "statistics": {
    "viewCount": unsigned long,
    "likeCount": unsigned long,
    "dislikeCount": unsigned long,
    "favoriteCount": unsigned long,
    "commentCount": unsigned long
  },