Skip to content

Instantly share code, notes, and snippets.

@edmorais
Last active December 14, 2015 11:59
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 edmorais/5083026 to your computer and use it in GitHub Desktop.
Save edmorais/5083026 to your computer and use it in GitHub Desktop.
oEmbed code snippet
<?php
class Util {
/**
* oEmbed
* Fetches embed codes for Vimeo or YouTube.
*
* $url: the video's URI.
* $maxwidth: embed width, in pixels (optional)
* $more_options: query string to append (optional)
*
* Returns Array with oEmbed data.
*/
public static function oEmbed($url, $maxwidth=false, $more_options='') {
global $Config;
$so = false;
$xml_url = false;
$oembed = false;
if (!$maxwidth) { $maxwidth = $Config['Width-L']; }
// is embed code cached? (uses my Cache class)
$so = Cache::load($url, 'oembed'.$maxwidth, 24*365); // 1 year half-life
if (!$so) {
$more_options = empty($more_options) ? '' : '&'.$more_options;
// Vimeo:
if(preg_match('/^http:\/\/.*?vimeo.com/', $url)) {
$options = http_build_query(array(
'url' => $url,
'width' => $maxwidth,
'portrait' => 'false',
'color' => 'CCCCCC',
), '', '&'); // PHP 5.3 requires the & separator to be set
// get it
$xml_url = 'http://www.vimeo.com/api/oembed.xml?'.$options.$more_options;
}
// Youtube:
if(preg_match('/^http:\/\/.*?youtube.com/', $url)) {
$options = http_build_query(array(
'url' => $url,
'maxwidth' => $maxwidth,
'iframe' => 1,
'format' => 'xml'
), '', '&'); // PHP 5.3 requires the & separator to be set
// get it
$xml_url = 'http://www.youtube.com/oembed?'.$options.$more_options;
}
// get it:
if ($xml_url) {
if ($oembed = @simplexml_load_file($xml_url)) {
$so = json_encode($oembed);
Cache::save($url, $so, 'oembed'.$maxwidth);
}
}
} else {
$oembed = json_decode($so);
}
return $oembed;
}
} // end class
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment