Skip to content

Instantly share code, notes, and snippets.

@tonythere
Created March 5, 2015 04:21
Show Gist options
  • Save tonythere/e66b0bb18f211fc7d385 to your computer and use it in GitHub Desktop.
Save tonythere/e66b0bb18f211fc7d385 to your computer and use it in GitHub Desktop.
<?php
/**
* @param String $url
* @return object
*/
function get_yt_info($url)
{
$id = get_yt_id($url);
if (is_null($id)) {
return Response::json(['error' => 'Link không hợp lệ!']);
}
$cacheKey = 'yt_' . $id;
$info = Cache::remember($cacheKey, 60, function () use ($id)
{
$endpoint = "https://gdata.youtube.com/feeds/api/videos/{$id}?v=2&alt=jsonc";
return json_decode(file_get_contents($endpoint));
});
return $info;
}
function get_yt_id($url)
{
if (strlen($url) == 11) {
return $url;
}
$regex = '#https?://(?:www\.)?(?:youtube\.com/watch\?v=|youtu\.be/)([a-zA-Z0-9\-_]{11})#i';
if (preg_match($regex, $url, $matches)) {
return $matches[1];
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment