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 :

Advertisements

How to search for vedio from the youtube and fetch the metadata

Purpose

This will help you to search the video from youtube according to your query. And you can get all the meta data for the fetch result.

Platform

-PHP

Prerequisites

php zend framework.

You can download the zend framework from the  http://framework.zend.com/downloads/latest .

Code Snippet

<?php

//path of you zend framework

set_include_path(‘C:/wamp/bin/php/zend_framework/library’ . PATH_SEPARATOR . get_include_path());

//default
require_once ‘Zend/Loader.php’;
Zend_Loader::loadClass(‘Zend_Gdata_YouTube’);

function searchAndPrint()
{

$yt = new Zend_Gdata_YouTube();
$query = $yt->newVideoQuery();

//search Key word
$query->videoQuery = ‘any thing you want to search’;

//search result start index
$query->startIndex = 1;

//number of results fetched
$query->maxResults = 8;

//order by creiteria
$query->orderBy = ‘viewCount’;

////category of your search
$query->category = ‘Trailers’;
$videoFeed = $yt->getVideoFeed($query);

?>
<?php

//print the result
foreach ($videoFeed as $videoEntry) {
echo “———VIDEO———-<br>”;
echo “Title: ” . $videoEntry->getVideoTitle() . “<br>”;
echo “\nDescription:<br>”;
echo $videoEntry->getVideoDescription();
echo “Video ID: ” . $videoEntry->getVideoId() . “<br>”;
echo ‘Updated: ‘ . $videoEntry->getUpdated() . “\n”;
echo ‘Category: ‘ . $videoEntry->getVideoCategory() . “\n”;
echo ‘Tags: ‘ . implode(“, “, $videoEntry->getVideoTags()) . “\n”;
echo ‘Watch page: ‘ . $videoEntry->getVideoWatchPageUrl() . “\n”;
echo ‘Flash Player Url: ‘ . $videoEntry->getFlashPlayerUrl() . “\n”;
echo ‘Duration: ‘ . $videoEntry->getVideoDuration() . “\n”;
echo ‘View count: ‘ . $videoEntry->getVideoViewCount() . “\n”;
echo ‘Rating: ‘ . $videoEntry->getVideoRatingInfo() . “\n”;
echo ‘Geo Location: ‘ . $videoEntry->getVideoGeoLocation() . “\n”;
echo ‘Recorded on: ‘ . $videoEntry->getVideoRecorded() . “\n”;

}

}

?>

Query Attributes Tutorials

https://developers.google.com/youtube/2.0/reference?csw=1#Searching_for_videos

https://developers.google.com/youtube/2.0/developers_guide_php?csw=1#SearchingVideos