How to Get Video Details from YouTube Video Details API in PHP

Get YouTube Video Details API: The YouTube API allows you to retrieve information about YouTube videos programmatically. In this post, we’ll walk through a PHP code snippet to fetch key details of a YouTube video using the API.

YouTube Video Details Extractor Demo

Prerequisites

  • PHP with cURL enabled
  • YouTube API key, if you do not have one, create it here

Get YouTube Video Details API Tutorial Steps

Step 1 – Include API Key

First, you’ll need a YouTube API key which identifies your application. You can get one by following this step-by-step guide to generate your YouTube API key.

Step 2 – Define the API Endpoint

The videos endpoint of the YouTube API allows retrieving data on specific videos. We’ll construct the URL for it.

Step 3 – Specify Video ID

Next, we need the ID of the YouTube video to look up. This can be extracted from the video URL.

Step 4 – Define Request Parameters

We specify the snippet part to get back title, description and other details. The video ID goes in the id parameter.

‘part’ => ‘snippet’, explanation

The ‘part‘ parameter in the YouTube API request allows you to specify which pieces of information you want to retrieve for a video.

The ‘snippet’ part returns basic details about the video like title, description, thumbnails, channel title etc.

Some other values you can use for the ‘part’ parameter are:

  • ‘contentDetails’ – returns information like video duration, definition, captions etc.
  • ‘statistics’ – returns metrics like view count, like count, comment count etc.
  • ‘topicDetails’ – returns topic categories the video is listed in.
  • ‘player’ – returns player related data like embed HTML code.
  • ‘status’ – returns information about upload status, privacy status etc.
  • ‘recordingDetails’ – returns recording location data if available.
  • ‘localizations’ – returns localized titles and descriptions.

So in summary, the ‘part’ parameter allows you to pick which video data sets you need returned by the API. ‘snippet’ gets the core details. You can request multiple ‘part’ values in comma separated format based on your requirements. For example, ‘part’ => ‘snippet,contentDetails,player,status’,

Step 5 – Build and Send Request

We build the request URL with http_build_query(), initialize cURL, and send the request.

Now, the above $url variable holds the final api endpoint url in the following format to fetch the video details from the YouTube API:

‘https://youtube.googleapis.com/youtube/v3/videos?part=snippet&id=VIDEO_ID&key=API_KEY

Step 6 – Process Response

We decode the JSON response into a PHP array and output it.

And we’re done! The snippet fetches and displays video details from the YouTube API in PHP. You can expand on it to build more complex applications.

Complete Code: Get YouTube Video Details API

Data Returned: YouTube Video Details API

If all goes well and there is no error in your code and your YouTube API key and endpoint are valid then you will get the video data in the following format.

You can see the official YouTube API documentation for the Videos API on this link.

Leave a Comment