class StreamProviderTwitch { const clipUidPattern = 'www\.twitch\.tv\/([^\"][a-zA-Z0-9-_]+)[&\"]\/embed\/'; const thumbJsonPattern = 'https://api.twitch.tv/kraken/streams/()'; private static function getUid( $code ) { $pattern = self::clipUidPattern; return preg_match("~{$pattern}~", $code, $match) ? $match[1] : null; } public static function getThumbUrl( $code ) { if ( ($uid = self::getUid($code)) !== null ) { $jsonUrl = str_replace('()', $uid, self::thumbJsonPattern); $fileCont = @file_get_contents($jsonUrl); if ( strlen($fileCont) ) { $metaObj = @json_decode($fileCont); if ( $metaObj ) { $url = @$metaObj->streams[0]->preview->medium; } } return !empty($url) ? $url : StreamProviders::PROVIDER_UNDEFINED; } return StreamProviders::PROVIDER_UNDEFINED; } }