Created
March 5, 2015 04:21
-
-
Save tonythere/e66b0bb18f211fc7d385 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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