How to Get Video Information from YouTube and Other Platforms in PHP

Recently, I came across this GitHub library which gives us the ability to get Video information from YouTube and other video platforms. It gives support for popular video platforms like YouTube, Dailymotion, Vimeo, Facebook, LiveLeak, and CDA. If you want to grab information like video title, description, tags(if any), etc. dynamically then this is the right package for you.

This library can be used in both Laravel and PHP projects. In this article, we study how to use this video information parser library in PHP.

Getting Started

For getting started, you need to install the library using the below command. To run this command make sure you have installed Composer on your system.

composer require chojnicki/video-platforms-parser

The library parser works with and without an API key. It means if we don’t pass the API key, this library grabs the video information by reading the HTML DOM of the page. In the case of API keys provided, the library gets video information by calling API endpoints of the respective video platforms.

Get Video Information from YouTube

Let’s take a look at how to grab details of YouTube videos using the API key and without an API key. You will get your API key by following the steps below.

  • Head over to the Google Developer Console https://console.developers.google.com.
  • Create a new project. Optionally, you can also select existing project.
  • Give name to your project. Google Console will create unique project ID for you.
  • Your newly created project will appear on top of the left sidebar.
  • Click on Library from the left menu. You will see a list of Google APIs. Enable the YouTube Data API.
  • Next, from the left menu click on the Credentials. Select API key under Create credentials.
  • Copy this API Key.
API key

Once you are ready with your API key, write the code as follows. It will gives you details of the specified YouTube video.

<?php
require_once "vendor/autoload.php";
 
use Chojnicki\VideoPlatformsParser\VideoPlatformsParser;
 
// For YouTube
$parser = new VideoPlatformsParser(['youtube_api_key' => 'PASTE_API_KEY_HERE']);
$info = $parser->get('YOUTUBE_VIDEO_URL');
print_r($info);

On printing the response, you will get details like video id, title, description, thumbnail and tags of a video.

Without API key code will be as follows.

<?php
require_once "vendor/autoload.php";
 
use Chojnicki\VideoPlatformsParser\VideoPlatformsParser;
 
// For YouTube
$parser = new VideoPlatformsParser(['youtube_api_disabled' => true]);
$info = $parser->get('YOUTUBE_VIDEO_URL');
print_r($info);

Same code will be used for Dailymotion, Vimeo videos.

<?php
require_once "vendor/autoload.php";
 
use Chojnicki\VideoPlatformsParser\VideoPlatformsParser;
 
// Vimeo
$parser = new VideoPlatformsParser();
$info = $parser->get('VIMEO_VIDEO_URL');
print_r($info);

// Dailymotion
$parser = new VideoPlatformsParser();
$info = $parser->get('DAILYMOTION_VIDEO_URL');
print_r($info);

This is how you got the details about the video using the respective platforms. The library also has support for Facebook, Twitter, CDA, etc. Read more about the package on their documentation.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

2 thoughts on “How to Get Video Information from YouTube and Other Platforms in PHP

Leave a Reply

Your email address will not be published.