Created
August 14, 2012 18:02
-
-
Save ryantology/3351315 to your computer and use it in GitHub Desktop.
Grab embed id for youtube / vimeo videos
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 if (preg_match('/http:\/\/www.youtube.com\/watch.*/', $this->request->data['Upload']['embed'], $matches)) { | |
parse_str(parse_url($this->request->data['Upload']['embed'],PHP_URL_QUERY),$vars); | |
$videoid = $vars['v']; | |
if (!$videoid) { break; } | |
$this->request->data['Upload']['type'] = 'youtube'; | |
$this->request->data['Upload']['attribution'] = $videoid; | |
$videoThumb = $this->_getThumbYoutube($videoid); | |
} | |
if (preg_match('/http:\/\/(www\.)?vimeo.com\/(\d+)/', $this->request->data['Upload']['embed'], $matches)) { | |
$videoid = $matches[2]; | |
$this->request->data['Upload']['type'] = 'vimeo'; | |
$this->request->data['Upload']['attribution'] = $videoid; | |
$videoThumb = $this->_getThumbVimeo($videoid); | |
} | |
if (preg_match('/http:\/\/(www\.)?vimeo.com\/channels\/.*#(\d+)/', $this->request->data['Upload']['embed'], $matches)) { | |
$videoid = $matches[2]; | |
$this->request->data['Upload']['type'] = 'vimeo'; | |
$this->request->data['Upload']['attribution'] = $videoid; | |
$videoThumb = $this->_getThumbVimeo($videoid); | |
} | |
function _getThumbYoutube($videoid) { | |
// Download image from http://img.youtube.com/vi/FkPBBZiMAjY/0.jpg | |
App::import('Core', 'HttpSocket'); | |
$HttpSocket = new HttpSocket(); | |
$request = array('uri' => 'http://img.youtube.com/vi/'.$videoid.'/0.jpg'); | |
$data = $HttpSocket->request($request); | |
if ($HttpSocket->response['status']['code'] == 200) { | |
return $data; | |
} else { | |
return False; | |
} | |
} | |
function _getThumbVimeo($videoid) { | |
// Get Video Data: http://vimeo.com/api/v2/video/5615843.json | |
// Videos.Video.thumbnail_large | |
App::import('Core', 'HttpSocket'); | |
$HttpSocket = new HttpSocket(); | |
$request = array('uri' => 'http://vimeo.com/api/v2/video/'.$videoid.'.json'); | |
$data = $HttpSocket->request($request); | |
if ($HttpSocket->response['status']['code'] != 200) { | |
return False; | |
} | |
$videoData = json_decode($data, TRUE); | |
$HttpSocket->reset(); | |
if(!is_object($videoData[0])) { | |
$request = array('uri' => $videoData[0]['thumbnail_large']); | |
} else { | |
$request = array('uri' => $videoData[0]->thumbnail_large); | |
} | |
$data = $HttpSocket->request($request); | |
if ($HttpSocket->response['status']['code'] != 200) { | |
return False; | |
} else { | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment