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);
@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