-
-
Save Kradre/31205ca3a3bdb5fbe51441bd08f683e3 to your computer and use it in GitHub Desktop.
Get video information from url and convert to array
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 | |
/** | |
* Get video information and convert to array | |
* @param string $url, the url of the video | |
* @uses wp_remote_get() | |
* @return array $video, information about the video | |
*/ | |
function get_video( $url ) { | |
$keyd = "AIzaSyBKpv0AKIID3m60mPSOb_SCW-dvOPWbqpY"; | |
$video = array(); | |
$split_url = parse_url( $url ); | |
if ( isset( $split_url['host'] ) ) { | |
$video['host'] = $split_url['host']; | |
// Set up youtube variables | |
if ( $video['host'] == 'www.youtube.com' || $video['host'] == 'youtube.com' ) { | |
$video_id = explode( '?v=', $url ); // For videos like watch?v=... | |
if ( empty( $video_id[1] ) ) { | |
$video_id = explode( '&v=', $url ); // For videos like watch?player&v= | |
if ( empty( $video_id[1] ) ) { | |
$video_id = explode( '/v/', $url ); // For videos like watch/v/ | |
} | |
} | |
$video_id = explode( '&', $video_id[1] ); // Deleting any other params | |
$video['id'] = $video_id[0]; | |
$video['embed'] = '<iframe id="ytplayer" type="text/html" width="960" height="720" src="http://www.youtube.com/embed/' . $video['id'] . '?autoplay=0&enablejsapi=1&wmode=transparent" frameborder="0"></iframe>'; | |
$video['thumbnail'] = 'http://img.youtube.com/vi/' . $video['id'] . '/0.jpg'; | |
$videodata = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=".$video['id']."&key=".$keyd); | |
$json = json_decode($videodata, true); | |
$video['desc'] = $json['items'][0]['snippet']['description']; | |
$video['title'] = $json['items'][0]['snippet']['title']; | |
// Set up vimeo variables | |
} elseif ( $video['host'] == 'www.vimeo.com' || $video['host'] == 'vimeo.com' ) { | |
$video_id = explode( '/', $url ); | |
$video['id'] = $video_id[3]; | |
$video['embed'] = '<iframe src="//player.vimeo.com/video/' . $video['id'] . '?title=0&byline=0&portrait=0" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'; | |
$vimeo = unserialize(file_get_contents("http://vimeo.com/api/v2/video/".$video['id'].".php")); | |
$video['thumbnail'] = $vimeo[0]['thumbnail_large']; | |
$video['title'] = $vimeo[0]['title']; | |
$video['desc'] = $vimeo[0]['description']; | |
} elseif ( $video['host'] == 'www.rutube.ru' || $video['host'] == 'rutube.ru' ) { | |
$video_id = explode( '/video/', $url ); // For videos like watch?v=... | |
$video_id = explode( '/?', $video_id[1] ); | |
$video['id'] = $video_id[0]; | |
$videodata = file_get_contents("http://rutube.ru/api/oembed/?url=https://rutube.ru/video/".$video['id']."/&format=json"); | |
$json = json_decode($videodata, true); | |
$video['embed'] = $json['html']; | |
$video['title'] = $json['title']; | |
$video['thumbnail'] = "http:".$json['thumbnail_url']; | |
} | |
// if all else fails, just return the embed | |
} else { | |
$video['embed'] = $url; | |
return false; | |
} | |
return $video; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment