How to Retrieve Youtube Video Details from Video Url Using PHP

How can I retrieve YouTube video details from video URL using PHP?

You can get data from youtube oembed interface in two formats: XML and JSON

Interface address: http://www.youtube.com/oembed?url=youtubeurl&format=json

Use this PHP function to get data

 function get_youtube($url){

$youtube = "http://www.youtube.com/oembed?url=". $url ."&format=json";

$curl = curl_init($youtube);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);
return json_decode($return, true);

}

$url = // youtube video url

// Display Data
print_r(get_youtube($url));

Don't forget to enable extension=php_curl.dll in your php.ini

Retrieve YouTube video details, including description from video URL using PHP?

In order to receive the description of a video you have 2 options.

  1. Use an API.
  2. Crawl the website.

The API that you need is under googleapis.com domain.

The url that you need to use is:

https://www.googleapis.com/youtube/v3/videos?id=VIDEO_ID&key=YOUR_API_KEY&fields=items(id,snippet(description))&part=snippet

Notice that you have to change the VIDEO_ID and YOUR_API_KEY.

To get an API key follow these instructions: link.


Building a web crawler is more complex.

Try following this tutorial to build your own web crawler here

Get YouTube video title with video ID in PHP

Use the YouTube API v3 and the list method, sending in the ID as a parameter and you will get information about that specific video, https://developers.google.com/youtube/v3/docs/videos/list#id.

GET https://www.googleapis.com/youtube/v3/videos

With the id parameter

string

The id parameter specifies a comma-separated list of the YouTube video ID(s) for the resource(s) that are being retrieved. In a video resource, the id property specifies the video's ID.

In your case, with the PHP SDK, the example would be something like:

# Call the videos.list method to retrieve location details for each video.
$videosResponse = $youtube->videos->listVideos('snippet, recordingDetails', array(
'id' => $videoIds,
));

How to get YouTube Videos Description and Comments with Youtube Api in php

First just put your

https://www.googleapis.com/youtube/v3/videos?part=snippet&id=0KNk-Joi-NM&key=my_api_key

in your browser and see if you get the correct response. If you don't see the following with your key, check your key's credentials. Works fine here with my key.

{
"kind": "youtube#videoListResponse",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/3EAsCBWH9776A-ORuySfGxdI1go\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"XI7nbFXulYBIpL0ayR_gDh3eu1k/mvwmsHAehTppyzBCqqEpnCIt2d4\"",
"id": "0KNk-Joi-NM",
"snippet": {
"publishedAt": "2018-05-19T06:30:28.000Z",
"channelId": "UCy436qLXlyLqddExVC64auw",
"title": "Tera Ghata | Gajendra Verma Ft. Karishma Sharma | Vikram Singh | Official Video",
"description": "Welcome to the Official Channel of Gajendra Verma\nBlive Music & Entertainment and Virtual Planet Present #GajendraVerma New Music Video #TeraGhata ft. Karishma Sharma Directed by Vikram Singh\n\nHear It Exclusively on Gaana : https://gaana.com/song/tera-ghata\nDownload Link : \n\nOperator Codes:\nAirtel Subscribers Dial 009165300001618\nAirtel Subscribers Dial 5432116531618\nVodafone Subscribers sms CT 10472118 to 56789\nVodafone Subscribers Dial 53710472118\nIdea Subscribers Dial 5678910472118\nDocomo Promo Code for Call Me Tunes sms SET 10472118 to 543211\nBSNL (N) NORTH Subscribers ZONE BT 7092421 to 56700\nBSNL (W) WEST Subscribers ZONE BT 7092421 to 56700\nBSNL (S) SOUTH Subscribers ZONE BT 10472118 to 56700\nBSNL (E) EAST Subscribers ZONE BT 10472118 to 56700\nVirgin Subscribers SMS 10472118 to 58475\nTelenor OnMobile Subscribers SMS ACT 10472118 to 543211\n\nSong: Tera Ghata\nAlbum: From Lost To Found\nSinger/Composer/Lyricist: Gajendra Verma\nLyricist: Gajendra Verma\n\nDirected by: Vikram Singh \nProducer: Sanjay Kukreja\nStarring : Karishma Sharma\nProduction: Virtual Planet Production\nCinematographer: Suman Dutta\nAssistant Director: Mansi Moghe, Vishal Yoman, Rohan Shah\nEditor: Hashtag# Studios\nColorist: Vikrant Studios (Nirmal Sharma)\nPublicity Design : Kalpanik Films\nMakeup & Hair: Aanchal Khanna\nStylist: Surubhi Sharma\nSocial Promotion: Ayushi Anand\nFocus Puller: Ramu Karri\nDrone: Aditya Pandey\nCamera Team: Vikas Jaiswal, Avishekh Patel\nLine Production: Destination Siam, Thailand, Emerald Cove\nOnline Promotions : Chitransh Jain & Shubham Jain\nRecord Label: Virtual Planet Music\n\n♫Social Handle ♫\nConnect with Gajendra Verma:\nFacebook /iVermaGajendra\nInstagram @iVermaGajendra\nTwitter @iVermaGajendra\n\nSubscribe for More Videos\nSubscribe: \nFacebook: http://www.facebook.com/virtualplanetmusic\nGoogle: http://www.google.com/+virtualplanetmusic\n\nWebsite\nhttp://www.virtualplanet.in\n\nFor Live Shows Bookings & Business Enquiries: \nPrashant Chaudhary +919988989833.\nPrashant Chaudhary +919988989833.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/0KNk-Joi-NM/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/0KNk-Joi-NM/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/0KNk-Joi-NM/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/0KNk-Joi-NM/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/0KNk-Joi-NM/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "Gajendra Verma",
"tags": [
"Gajendra verma",
"Gajendra verma songs",
"Vikram Singh",
"Gajendra verma Official",
"Virtual Planet Music",
"tera ghata",
"tera",
"ghata",
"karishma sharma",
"gajendra verma",
"gajendra verma song",
"gajendra verma all songs",
"gajendra verma new song",
"karishma sharma song",
"latest bollywood songs",
"gajendra verma songs",
"latest hindi songs",
"viral song",
"tera ghata mera kuch nahi jata",
"tera ghata viral video",
"isme tera ghata mera kuch nahi jata",
"isme tera ghata viral video"
],
"categoryId": "10",
"liveBroadcastContent": "none",
"localized": {
"title": "Tera Ghata | Gajendra Verma Ft. Karishma Sharma | Vikram Singh | Official Video",
"description": "Welcome to the Official Channel of Gajendra Verma\nBlive Music & Entertainment and Virtual Planet Present #GajendraVerma New Music Video #TeraGhata ft. Karishma Sharma Directed by Vikram Singh\n\nHear It Exclusively on Gaana : https://gaana.com/song/tera-ghata\nDownload Link : \n\nOperator Codes:\nAirtel Subscribers Dial 009165300001618\nAirtel Subscribers Dial 5432116531618\nVodafone Subscribers sms CT 10472118 to 56789\nVodafone Subscribers Dial 53710472118\nIdea Subscribers Dial 5678910472118\nDocomo Promo Code for Call Me Tunes sms SET 10472118 to 543211\nBSNL (N) NORTH Subscribers ZONE BT 7092421 to 56700\nBSNL (W) WEST Subscribers ZONE BT 7092421 to 56700\nBSNL (S) SOUTH Subscribers ZONE BT 10472118 to 56700\nBSNL (E) EAST Subscribers ZONE BT 10472118 to 56700\nVirgin Subscribers SMS 10472118 to 58475\nTelenor OnMobile Subscribers SMS ACT 10472118 to 543211\n\nSong: Tera Ghata\nAlbum: From Lost To Found\nSinger/Composer/Lyricist: Gajendra Verma\nLyricist: Gajendra Verma\n\nDirected by: Vikram Singh \nProducer: Sanjay Kukreja\nStarring : Karishma Sharma\nProduction: Virtual Planet Production\nCinematographer: Suman Dutta\nAssistant Director: Mansi Moghe, Vishal Yoman, Rohan Shah\nEditor: Hashtag# Studios\nColorist: Vikrant Studios (Nirmal Sharma)\nPublicity Design : Kalpanik Films\nMakeup & Hair: Aanchal Khanna\nStylist: Surubhi Sharma\nSocial Promotion: Ayushi Anand\nFocus Puller: Ramu Karri\nDrone: Aditya Pandey\nCamera Team: Vikas Jaiswal, Avishekh Patel\nLine Production: Destination Siam, Thailand, Emerald Cove\nOnline Promotions : Chitransh Jain & Shubham Jain\nRecord Label: Virtual Planet Music\n\n♫Social Handle ♫\nConnect with Gajendra Verma:\nFacebook /iVermaGajendra\nInstagram @iVermaGajendra\nTwitter @iVermaGajendra\n\nSubscribe for More Videos\nSubscribe: \nFacebook: http://www.facebook.com/virtualplanetmusic\nGoogle: http://www.google.com/+virtualplanetmusic\n\nWebsite\nhttp://www.virtualplanet.in\n\nFor Live Shows Bookings & Business Enquiries: \nPrashant Chaudhary +919988989833.\nPrashant Chaudhary +919988989833."
}
}
}
]
}

PHP Regex to get youtube video ID?

Use parse_url() and parse_str().

(You can use regexes for just about anything, but they are very easy to make an error in, so if there are PHP functions specifically for what you are trying to accomplish, use those.)

parse_url takes a string and cuts it up into an array that has a bunch of info. You can work with this array, or you can specify the one item you want as a second argument. In this case we're interested in the query, which is PHP_URL_QUERY.

Now we have the query, which is v=C4kxS1ksqtw&feature=relate, but we only want the part after v=. For this we turn to parse_str which basically works like GET on a string. It takes a string and creates the variables specified in the string. In this case $v and $feature is created. We're only interested in $v.

To be safe, you don't want to just store all the variables from the parse_url in your namespace (see mellowsoon's comment). Instead store the variables as elements of an array, so that you have control over what variables you are storing, and you cannot accidentally overwrite an existing variable.

Putting everything together, we have:

<?php
$url = "http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=relate";
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
echo $my_array_of_vars['v'];
// Output: C4kxS1ksqtw
?>

Working example


Edit:

hehe - thanks Charles. That made me laugh, I've never seen the Zawinski quote before:

Some people, when confronted with a problem, think ‘I know, I’ll use regular expressions.’ Now they have two problems.
Jamie Zawinski

Fetch data from youtube video list

YouTube Data API request returns the JSON data that includes the information of the video (title, description, thumbnails, publish date, etc.). You need to specify the API key ($API_key), YouTube Channel ID ($channelID).

//Get videos from channel by YouTube Data API
$API_key = 'Insert_Your_API_Key';
$channelID = 'Insert_Channel_ID';
$maxResults = 10;

$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));

Youtube Video List using PHP
Loop through the $videoList->items to list the videos from a YouTube channel.

foreach($videoList->items as $item){
//Embed video
if(isset($item->id->videoId)){
echo '<div class="youtube-video">
<iframe width="280" height="150" src="https://www.youtube.com/embed/'.$item->id->videoId.'" frameborder="0" allowfullscreen></iframe>
<h2>'. $item->snippet->title .'</h2>
</div>';
}
}

In the YouTube video list, Video ID and Title are used, but you can show the other information as per your requirement. The following information is provided by the API.

  • YouTube Video ID – $item->id->videoId
  • YouTube Video Publish Date – $item->snippet->publishedAt
  • YouTube Channel ID – $item->snippet->channelId
  • YouTube Video Title – $item->snippet->title
  • YouTube Video Description – $item->snippet->description
  • YouTube Video Thumbnail URL (default size) – $item->snippet->thumbnails->default->url
  • YouTube Video Thumbnail URL (medium size) – $item->snippet->thumbnails->medium->url
  • YouTube Video Thumbnail URL (large size) – $item->snippet->thumbnails->high->url
  • YouTube Channel Title – $item->snippet->channelTitle


Related Topics



Leave a reply



Submit