Guides in this series:
- Part 1: Initial setup
- Part 2: Get video details
- Part 3: Get all videos from a channel
- Part 4: OAuth Connection
- Part 5: Edit video details
Intro
In this guide we’ll talk about retrieving the details of a video from the YouTube data API using PHP.
Prerequisites:
- A project set up in the Google Cloud console
- The package google-api-php-client.
For full details on setting up the above prerequisites, check out my first part of this series: YouTube API & PHP - Initial setup.
After following the instructions in that video, you’ll have the following code which is the example we’ll build upon:
<?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
From this code, we want to focus on this line:
$response = $service->videos->listVideos('snippet', ['id' => 'fG08dcJ8xFE']);
This lines uses the videos.list method which you can read about in the YouTube data API docs: https://developers.google.com/youtube/v3/docs/videos/list.
Required parameter 1: part
The first required parameter for the list videos method is the part
. The part
parameter expects a comma-separated list of one or more video resource properties that the API response will include.
Part options include:
-
contentDetails
-
fileDetails
-
id
-
liveStreamingDetails
-
localizations
-
player
-
processingDetails
-
recordingDetails
-
snippet
-
statistics
-
status
-
suggestions
-
topicDetails
In our example, the part
we request is snippet
which returns us the following information:
-
channelId
-
title
-
description
-
tags
-
categoryId
You can specify multiple parts by providing a comma separated string. Example retrieving the parts snippet
and statistics
:
$response = $service->videos->listVideos(
'snippet,statistics',
['id' => 'fG08dcJ8xFE']
);
Required parameter 2: filter
The next required parameter is a filter that specifies which video(s) you want to receive. Options here include:
-
chart
(e.g.mostPopular
) -
id
(of a specific video) -
myRating
(Set tolike
ordislike
). This filter requires an OAuth connection which we’ll set up in future videos.
In our example, we filtered by id
:
$response = $service->videos->listVideos(
'snippet',
['id' => 'fG08dcJ8xFE']
);
Optional parameters
After the two required parameters, there are a series of optional parameters that can influence your query. You can read more about these parameters in the docs.
Here’s an example with the addition of the maxResults
optional parameter:
$response = $service->videos->listVideos(
'snippet',
['chart' => 'mostPopular', 'maxResults' => 50]
);
Output the response
Because our query is designed to return a single video, we can update our code to narrow down on that video from the response:
$response = $service->videos->listVideos(
'snippet',
['id' => 'fG08dcJ8xFE']
);
# Narrow down to the single video within the response
$video = $response->items[0]->snippet;
Then, we can construct some output to show the details of this video:
<h1>Example: Get details for a single video</h1>
<h2>Title</h2>
<?php echo $video->title ?>
<h2>Thumbnail</h2>
<img
src='<?php echo $video->thumbnails->high->url ?>'
style='width:150px'
alt='Thumbnail for the video <?php echo $video->title ?>'>
<h2>Description</h2>
<textarea style='width:700px; height:200px'><?php echo $video->description ?></textarea>
Results: