← Other topics

YouTube API & PHP - Initial setup (google-api-php-client)

Video Notes

Guides in this series:

Intro

Welcome to my series on using the YouTube API with PHP.

In this first part of the series, we’ll cover the following setup steps:

  1. Create a new project via the Google Cloud console, which will get us access to the YouTube API.
  2. Set up the package google-api-php-client which will provide us with PHP code can use to interact with the API.

In subsequent videos, we’ll put this setup to work and show examples of working with the YouTube API covering topics such as:

  • Getting the details for a single video
  • Getting all the public videos for a channel
  • Establishing an OAuth connection so we can edit data via the API
  • Updating a video’s details
  • Plus more - comment on the video if there’s a particular topic you want to see covered

Google Cloud console - New Project

To get started, visit the Google Cloud console at https://console.cloud.google.com/welcome. Click the dropdown on the top to see your list of projects.

See list of projects in the Google Cloud console

From the screen that appears, click NEW PROJECT. Create a new project in the Google Cloud console

Give your project a name and click CREATE. Naming a new project in the Google Cloud console

Google Cloud console - Generate API key

Next, under API & Services, locate your Credentials (https://console.cloud.google.com/apis/credentials). Once here, click CREATE CREDENTIALS and choose API KEY.

Creating an API key in the Google Cloud console

Copy the key it generates for you. Copying the API key that is generated for you in the Google Cloud console

Because in our first examples we’ll just be reading data (no updates) from the API, we only need an API Key. Later, when we look at updating data, we’ll need to set up OAuth credentials.

Google Cloud console - Enable YouTube API

Also under API & Services, locate Enabled APIs & Services, then click + ENABLE APIS AND SERVICES.

On the page that appears, search YouTube, choose YouTube Data API v3 and then click Enable.

Enable the YouTube Data API in the Google Cloud Console

Code setup

Switching gears to the code, we need to pull in the package google-api-php-client which will give us the utilities we need to connect to the YouTube API. This can be done via the following Composer command run in the root of your project:

> composer require google/apiclient

In my example, I’ll also pull in the package symfony/var-dumper so I can easily output data we’ll get back from YouTube during development using a dump function. If your code base already has a dump/debugging output utility you can skip this step.

> composer require symfony/var-dumper

Finally, I’ll set up a file with the following code which will utilize the google-api-php-client package to initialize an API connection with YouTube. Be sure to update this code replacing YOUR_API_KEY with the API key you retrieved in the above steps.

<?php

# You can omit requiring Composer’s autoload file if you're working in a framework (e.g. Laravel) that sets up autoloading for you
require_once 'vendor/autoload.php';

use Google\Client;
use Google\Service\YouTube;

# Configs
$apiKey = 'YOUR_API_KEY';

# Initialize YouTube API client
$client = new Client();
$client->setDeveloperKey($apiKey);
$service = new YouTube($client);

# Example query just to make sure we can connect to the API
$response = $service->videos->listVideos('snippet', ['id' => 'fG08dcJ8xFE']);

# Output the response to confirm it worked
dump($response);

# If you don’t have access to a dump function, replace the above `dump` with PHP’s built-in `var_dump` function

If your connection to the YouTube API is successful, you should see output like this:

Successful connection to the YouTube API

If the connection fails, you’ll see an error message indicating what the problem is.

What next?

Now that we've set up and demonstrated we can connect to the YouTube API, we can start to put it to work. In the next video in this series we'll take a closer look at the code we wrote above to retrieve the details on a single video and learn how to utilize the YouTube API docs.

Next: YouTube API & PHP - Get video details (videos.list)

If this info helped you out you can say thanks and support future content by clicking my Amazon affiliate link: https://amzn.to/3UtgnYk. If you make a purchase on Amazon within 24 hours of clicking the link I may receive a micro-commission and it costs you nothing extra. Any income from these sales goes directly to supporting me in making new videos and guides. Thank you for your support!

← Other topics