Get YOUTUBE video details from the URL of the video.

Summary : This code is to get the information of the youtube video using the URL of the Video.

Language: PHP
Require JSON parsing or XML parsing .

Note : information can be get from url,we are taking json format

URL FORMAT http://www.youtube.com/oembed?url=%5BURL of the video]&format=xml[or json]
&format will decide the output of the query.

Information to be fetched; Title, thumbnail, author name , provider name , author url, etc.

PHP code to print the data :

//url of the video you want to fetch the data.
$url = ‘https://www.youtube.com/watch?v=RalvLaiSZa0’;
$youtube = ‘http://www.youtube.com/oembed?url=’.$url.’&format=json’;

//get the json content, we have put ‘@’ to suppress the warning when url is not correct.
$content = @file_get_contents($youtube);
if($content === false){
echo “Some Error in URL , pls check it.Pass the valid URL”;
}else{
/parse the json content
$data = json_decode($content, TRUE);

//get all the required content fron data, it is an array contain all the information.
echo $data[‘title’];
//echo $data[‘html’];
echo $data[‘author_url’];
echo $data[‘provider_name’];
echo $data[‘provider_url’];
echo $data [‘thumbnail_url’];

//there may be cases when any of these information may not present , in that case you have to handle it using isset.
if(isset($data[‘type’])){
echo $data[‘type’];
}

}

///method got the same if you want to reuse the code .

getVideoDetailFromUrl($url){

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

//get the json content
$content = @file_get_contents($youtube);
if($content === false){
echo “Some Error in URL , pls check it.Pass the valid URL”;
}else{
//parse the json content
$data = json_decode($content, TRUE);
return $data;
}
}

 

NOTE: Don’t forget to Enable extension=php_curl.dll in your php.ini
To Test it online you can use “http://codepad.viper-7.com/” to run the code .

NEXT CODE : GET ALL THE VIDEO FROM THE AUTHOR OF PARTICULAR VIDEO.[AUTHOR DETAIL ALSO].

 

Important URL may be connected