Skip to content

Instantly share code, notes, and snippets.

@morales2k
Created July 31, 2014 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morales2k/86b133364197e6c367a1 to your computer and use it in GitHub Desktop.
Save morales2k/86b133364197e6c367a1 to your computer and use it in GitHub Desktop.
Get youtube video info and embed code from input youtube url. Can be easily modified to use different "part" queries... but I'm doing just the snippet and player ones here...
<?php
class videoData{
static $videoId = '';
static $videoData = array();
static private $apiKey = '[YOUR_API_KEY_HERE]';
static $url = 'https://www.googleapis.com/youtube/v3/videos?id=@@VIDEOID@@&part=snippet,player&key=@@APIKEY@@';
static $lastURL = '';
static function getIdFromURL($url){
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $possible_id);
self::$videoId = $possible_id[1];
return self::$videoId;
}
static function getId(){
return self::$videoId;
}
static function fetchData(){
try{
$url = self::$url;
$url = str_replace("@@VIDEOID@@", self::$videoId, $url);
$url = str_replace("@@APIKEY@@", self::$apiKey, $url);
$data = get_curl($url);
self::$lastURL = $url;
}catch(Exception $e){
return $e->getMessage();
}
self::$videoData = json_decode($data, true, JSON_FORCE_OBJECT | JSON_NUMERIC_CHECK);
}
static function getData(){
self::fetchData();
return self::$videoData;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment