Skip to content

Instantly share code, notes, and snippets.

@djekl
Created June 21, 2013 15:50
Show Gist options
  • Save djekl/5832149 to your computer and use it in GitHub Desktop.
Save djekl/5832149 to your computer and use it in GitHub Desktop.
Get Video Details from YouTube or Vimeo
<?php
$urls = array();
$videos = array();
// vimeo test
$urls[] = 'http://vimeo.com/6271487';
$urls[] = 'http://vimeo.com/68546202';
// youtube test
$urls[] = 'https://www.youtube.com/watch?v=xHRkHFxD-xY&list=RD022g5PnydsOrY';
$urls[] = 'https://www.youtube.com/watch?v=Mtn6KqO3RcA&list=RD022g5PnydsOrY';
foreach ($urls as $url) {
$videos[] = getVideoDetails($url);
}
function getVideoDetails($url)
{
$host = explode('.', str_replace('www.', '', strtolower(parse_url($url, PHP_URL_HOST))));
$host = isset($host[0]) ? $host[0] : $host;
switch ($host) {
case 'vimeo':
$video_id = substr(parse_url($url, PHP_URL_PATH), 1);
$hash = json_decode(file_get_contents("http://vimeo.com/api/v2/video/{$video_id}.json"));
// header("Content-Type: text/plain");
// print_r($hash);
// exit;
return array(
'provider' => 'Vimeo',
'title' => $hash[0]->title,
'description' => str_replace(array("<br>", "<br/>", "<br />"), NULL, $hash[0]->description),
'description_nl2br' => str_replace(array("\n", "\r", "\r\n", "\n\r"), NULL, $hash[0]->description),
'thumbnail' => $hash[0]->thumbnail_large,
'video' => "https://vimeo.com/" . $hash[0]->id,
'embed_video' => "https://player.vimeo.com/video/" . $hash[0]->id,
);
break;
case 'youtube':
preg_match("/v=([^&#]*)/", parse_url($url, PHP_URL_QUERY), $video_id);
$video_id = $video_id[1];
$hash = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$video_id}?v=2&alt=jsonc"));
// header("Content-Type: text/plain");
// print_r($hash);
// exit;
return array(
'provider' => 'YouTube',
'title' => $hash->data->title,
'description' => str_replace(array("<br>", "<br/>", "<br />"), NULL, $hash->data->description),
'description_nl2br' => str_replace(array("\n", "\r", "\r\n", "\n\r"), NULL, nl2br($hash->data->description)),
'thumbnail' => $hash->data->thumbnail->hqDefault,
'video' => "http://www.youtube.com/watch?v=" . $hash->data->id,
'embed_video' => "http://www.youtube.com/v/" . $hash->data->id,
);
break;
}
}
header("Content-Type: text/plain");
print_r($videos);
@bkrajendra
Copy link

bkrajendra commented Sep 22, 2018

Ive updated the function

	function getVideoDetails($url)
	{
	    $host = explode('.', str_replace('www.', '', strtolower(parse_url($url, PHP_URL_HOST))));
	    $host = isset($host[0]) ? $host[0] : $host;
            Get Vimeo API Key https://developer.vimeo.com/apps
            $vimeo_api_key="";
            $youtube_api_key = "";
            Get Youtube API Key https://developers.google.com/youtube/v3/getting-started
           

	    switch ($host) {
	        case 'vimeo':
	            $video_id = substr(parse_url($url, PHP_URL_PATH), 1);
                   
				$options = array('http' => array(
					'method'  => 'GET',
					'header' => 'Authorization: Bearer '.$vimeo_api_key
				));
				$context  = stream_context_create($options);

	            $hash = json_decode(file_get_contents("https://api.vimeo.com/videos/{$video_id}",false, $context));
	            header("Content-Type: text/plain");
				//echo $hash->description;
	            //var_dump($hash);
	            //exit;



	            return array(
	                    'provider'          => 'Vimeo',
	                    'title'             => $hash->name,
	                    'description'       => str_replace(array("<br>", "<br/>", "<br />"), NULL, $hash->description),
	                    'description_nl2br' => str_replace(array("\n", "\r", "\r\n", "\n\r"), NULL, $hash->description),
	                    'thumbnail'         => $hash->pictures->sizes[0]->link,
	                    'video'             => $hash->link,
	                    'embed_video'       => "https://player.vimeo.com/video/" . $video_id,
	                );
	            break;
	        case 'youtube':

	            preg_match("/v=([^&#]*)/", parse_url($url, PHP_URL_QUERY), $video_id);
	            $video_id = $video_id[1];
	            $hash = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=".$video_id."&key=".$youtube_api_key.""));
	            header("Content-Type: text/plain");

				//echo $hash->items[0]->snippet->description;
	            // print_r($hash);
	            //exit;
	            return array(
	                    'provider'          => 'YouTube',
	                    'title'             => $hash->items[0]->snippet->title,
	                    'description'       => str_replace(array("", "<br/>", "<br />"), NULL, $hash->items[0]->snippet->description),
	                    'description_nl2br' => str_replace(array("\n", "\r", "\r\n", "\n\r"), NULL, nl2br($hash->items[0]->snippet->description)),
	                    'thumbnail'         => 'https://i.ytimg.com/vi/'.$hash->items[0]->id.'/default.jpg',
	                    'video'             => "http://www.youtube.com/watch?v=" . $hash->items[0]->id,
	                    'embed_video'       => "http://www.youtube.com/embed/" . $hash->items[0]->id,
	                );
	            break;
	    }
	}

@punchcreative
Copy link

Was just looking for a solution like this.
Still does the job
Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment