,

Google API PHP | How to use Google API like YouTube Data v3 API in PHP

Google API’s provides a lot of data which is a part of most of the developers like Google Maps, YouTube API, Translation, etc. We usually make and handle Google API requests for various services in Javascript using client-side Google API libraries. Google API provides a lot of platforms and languages using which we can implement…

By.

•

min read

Google API’s provides a lot of data which is a part of most of the developers like Google Maps, YouTube API, Translation, etc. We usually make and handle Google API requests for various services in Javascript using client-side Google API libraries.

Google API provides a lot of platforms and languages using which we can implement on client or server side like JavaScript, Java, PHP, Python, etc.

In this post, we will discuss How to use Google API on server-side languages like PHP? Some times a developer may want to manipulate the results from multiple API calls and get a single result set. In such cases, we can easily do that in PHP by making API calls then return results according to the needs of the application.

Here we will take an example of YouTube Data v3 API which is a part of Google API’s

Let’s get started…

To communicate with various API’s using PHP, Google has already built Google API PHP Client, which makes API calls possible using PHP

Step 1) Download and Install Composer

Composer is an application-level package manager for the PHP programming language which is similar to NPM

Step 2) Download google-api-php-client using composer

Now create a file composer.json then replace the below configuration in this file

{
  "require": {
    "google/apiclient": "^2.0"
  }
}

Next, open CMD command prompt in the same folder then run following command. This command will download files for Google PHP Client

$ composer update

 

Step 3) Create a YouTube Sample API PHP file

We are ready to test the YouTube API to Search YouTube videos. Create a new file sample.php then paste below code in it.

<?php

/**
 * Sample PHP code for youtube.search.list
 * See instructions for running these code samples locally:
 * https://developers.google.com/explorer-help/guides/code_samples#php
 */

if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
  throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName('API code samples');
$client->setDeveloperKey('YOUR_API_KEY');

// Define service object for making API requests.
$service = new Google_Service_YouTube($client);

$queryParams = [
    'maxResults' => 25,
    'q' => 'surfing'
];

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

You can also check more such code from this link, just click on the tag icon to get code details

That’s it now you will be able to make API calls for YouTube DATA v3

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *