Created
July 31, 2014 16:25
-
-
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...
This file contains hidden or 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 | |
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