How to fetch YouTube video information using YouTube data API Using PHP

949

In this tutorial we are going to discuss about how to fetch youtube video information like video title, description, images, view count, like count, dislike count, favorite count, comment count etc.

This tutorial will explain you the basic use of YouTube data API with PHP. You can do lot more by this api to implement playlist, search, Channels etc.

First of all you need to create API key to authenticate your program with youtube.
Login In into google developer account https://console.developers.google.com

After that select You-tube data API and create one. After that you’ll able to see your API key.

Create a php script to fetch video information where you have to put your youtube video id and parts your going to fetch.
for example:-
https://www.youtube.com/watch?v=7UMNz5T-0VQ
In this URL your youtube video id is showing in red color.

info.php

<?php
$data = file_get_contents("https://www.googleapis.com/youtube/v3/videos?id=YOUTUBE_VIDEO_ID&key=YOUR_YOUTUBE_APIKEY&part=snippet,contentDetails,statistics,status");
$result = json_decode($data, true);
echo "<pre>";
print_r($result);
?>

 

After hitting the URL you can see response in array format, Now you can use this data according to your need.

Array
(
    [kind] => youtube#videoListResponse
    [etag] => "iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/A8gUd_KQ1uZMySq4P0ZdpAr5l9o"
    [pageInfo] => Array
        (
            [totalResults] => 1
            [resultsPerPage] => 1
        )
 
    [items] => Array
        (
            [0] => Array
                (
                    [kind] => youtube#video
                    [etag] => "iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/7dr0Ic-NMXY4EbRYD34CMORrFPk"
                    [id] => 7UMNz5T-0VQ
                    [snippet] => Array
                        (
                            [publishedAt] => 2012-01-25T23:40:33.000Z
                            [channelId] => UCY0KHYwwYLMh-NFCJAA0gFw
                            [title] => Introduction To PHP - A Complete PHP Tutorial for Absolute Beginners
                            [description] => This is an introduction to PHP. It is a part of a series of complete tutorials on learning PHP as an absolute beginner. If you're interested in becoming a professional PHP developer and don't have any prior programming experience these tutorials might help you on the fast track to becoming a professional PHP programmer.
 
Tutorial 1:
 
In this tutorial I will cover the basics such as what is PHP, some past, present and future notes on PHP and what it's used for as well as how it works. We'll also touch on a few topics to be discussed in further detail in the coming tutorials. Please feel free to leave your comments, questions, suggestions.
 
The slides are available at http://sheriframadan.com/phpcorner
                            [thumbnails] => Array
                                (
                                    [default] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/7UMNz5T-0VQ/default.jpg
                                            [width] => 120
                                            [height] => 90
                                        )
 
                                    [medium] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/7UMNz5T-0VQ/mqdefault.jpg
                                            [width] => 320
                                            [height] => 180
                                        )
 
                                    [high] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/7UMNz5T-0VQ/hqdefault.jpg
                                            [width] => 480
                                            [height] => 360
                                        )
 
                                    [standard] => Array
                                        (
                                            [url] => https://i.ytimg.com/vi/7UMNz5T-0VQ/sddefault.jpg
                                            [width] => 640
                                            [height] => 480
                                        )
 
                                )
 
                            [channelTitle] => Sherif Ramadan
                            [tags] => Array
                                (
                                    [0] => Tutorial PHP Programming
                                    [1] => Absolute PHP Beginner
                                    [2] => Professional PHP
                                    [3] => Web Development
                                    [4] => PHP Development
                                )
 
                            [categoryId] => 27
                            [liveBroadcastContent] => none
                            [localized] => Array
                                (
                                    [title] => Introduction To PHP - A Complete PHP Tutorial for Absolute Beginners
                                    [description] => This is an introduction to PHP. It is a part of a series of complete tutorials on learning PHP as an absolute beginner. If you're interested in becoming a professional PHP developer and don't have any prior programming experience these tutorials might help you on the fast track to becoming a professional PHP programmer.
 
Tutorial 1:
 
In this tutorial I will cover the basics such as what is PHP, some past, present and future notes on PHP and what it's used for as well as how it works. We'll also touch on a few topics to be discussed in further detail in the coming tutorials. Please feel free to leave your comments, questions, suggestions.
 
The slides are available at http://sheriframadan.com/phpcorner
                                )
 
                        )
 
                    [contentDetails] => Array
                        (
                            [duration] => PT19M46S
                            [dimension] => 2d
                            [definition] => sd
                             => false
                            [licensedContent] => 1
                        )
 
                    [status] => Array
                        (
                            [uploadStatus] => processed
                            [privacyStatus] => public
                            [license] => youtube
                            [embeddable] => 1
                            [publicStatsViewable] => 
                        )
 
                    [statistics] => Array
                        (
                            [viewCount] => 164870
                            [likeCount] => 633
                            [dislikeCount] => 32
                            [favoriteCount] => 0
                            [commentCount] => 103
                        )
 
                )
 
        )
 
)

You can learn more about youtube data api from https://developers.google.com/youtube/v3/getting-started

Thanks :)