Search code examples
phpgoogle-apiyoutube-apiyoutube-data-apigoogle-api-php-client

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


I am getting list of videos from youtube API. I want to also get views and comment count in response from youtube apis.

I am trying the following.

$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 Response

[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

I am getting vidoes title, description etc is it possible to get video's views & comments count in same api response?

Help me pls. Thank you


Solution

  • I am getting vidoes title, description etc is it possible to get video's views & comments count in same api response?

    No it is not posible.

    Search.list method

    You are using the 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 method

    To get a rating back you would need to use videos.list which will return a video resource which does contain the stats your are looking for.

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