public
Created

My first attempt at creating an OEmbed interface for SilverStripe

  • Download Gist
OembedService.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
/*
My first attempt at creating an OEmbed interface for SilverStripe
*/
 
 
<?php
 
/**
* Create an interface to load Oembed embed data
*
* @see [url for docs]
* @todo Finish a working version
*/
class OembedService extends RestfulService {
/**
* True if format request is formatted as an extension
* example: http://www.vimeo.com/api/oembed.{format}
* otherwise, defaults to querystring format request
*
* @var bool
*/
private $format_as_extension;
/**
* OembedObject with oembed data
*
* @var OembedObject
*/
private $oembed_object;
function __construct($oembed_endpoint_url, $format_as_extension=false) {
$this->baseURL = $oembed_endpoint_url;
$this->format_as_extension = $format_as_extension;
}
// format data for use with RestfulService
function request($url = '', $format='xml', $params=null, $method = "GET", $data = null, $headers = null) {
$query_string_params = array();
// add additional parameters if necessary
if (!is_null($params) && is_array($params)) {
$query_string_params = $params;
}
 
// set proper format request
if ($this->format_as_extension) { $this->baseURL .= '.' . $format; }
else { $query_string_params['format'] = $format; }
 
// add the url as parameter, oembed-style
$query_string_params['url'] = $url;
$this->setQueryString($query_string_params);
 
return parent::request('', $method, $data, $headers);
}
function requestOembedObject($url, $format='xml', $params=null) {
//have to make a custom XML object
try {
$response = $this->request($url, $format, $params);
// Debug::show($response);
$this->oembed_object = new OembedObject($response);
return true;
} catch (Exception $e) {
user_error("Error occurred in processing oembed response");
return false;
}
}
function getEmbedCode() {
return $this->oembed_object->getHtml();
}
}
 
class OembedObject extends Object {
// attributes. these are all based on the oembed spec
// <http://www.oembed.com/>
private $original_xml_data;
private $type;
private $version;
private $title;
private $author_name;
private $author_url;
private $provider_name;
private $provider_url;
private $cache_age;
private $thumbnail_url;
private $thumbnail_width;
private $thumbnail_height;
// additionl arguments, optional depending on type
private $url;
private $width;
private $height;
private $html;
function __construct($xml) {
$this->original_xml_data = $xml;
$this->parseOembedXml();
}
private function parseOembedXml() {
try {
$sxml = new SimpleXMLElement($this->original_xml_data->getBody());
$this->type = (string)$sxml->type;
$this->version = (string)$sxml->version;
$this->title = (string)$sxml->title;
$this->author_name = (string)$sxml->author_name;
$this->author_url = (string)$sxml->author_url;
$this->provider_name = (string)$sxml->provider_name;
$this->provider_url = (string)$sxml->provider_url;
$this->cache_age = (string)$sxml->cache_age;
$this->thumbnail_url = (string)$sxml->thumbnail_url;
$this->thumbnail_width = (string)$sxml->thumbnail_width;
$this->thumbnail_url = (string)$sxml->thumbnail_url;
switch($this->type) {
case 'photo':
$this->url = (string)$sxml->url;
$this->width = (string)$sxml->width;
$this->height = (string)$sxml->height;
break;
case 'video':
$this->html = (string)$sxml->html;
$this->width = (string)$sxml->width;
$this->height = (string)$sxml->height;
break;
case 'rich':
$this->url = (string)$sxml->html;
$this->width = (string)$sxml->width;
$this->height = (string)$sxml->height;
break;
default:
break;
}
return true;
} catch (Exception $e) {
user_error("Error occurred in processing Oembed xml");
return false;
}
}
// getters
function getOriginalXmlData() { return $this->original_xml_data; }
function getType() { return $this->type; }
function getVersion() { return $this->version; }
function getTitle() { return $this->title; }
function getAuthorName() { return $this->author_name; }
function getAuthorUrl() { return $this->author_url; }
function getProviderName() { return $this->provider_name; }
function getProviderUrl() { return $this->provider_url; }
function getCacheAge() { return $this->cache_age; }
function getThumbnailUrl() { return $this->thumbnail_url; }
function getThumbnailWidth() { return $this->thumbnail_width; }
function getThumbnailHeight() { return $this->thumbnail_height; }
// optional getters
function getUrl() { if(isset($this->url)) { return $this->url; }}
function getWidth() { if(isset($this->width)) { return $this->width; }}
function getHeight() { if(isset($this->height)) { return $this->height; }}
function getHtml() { if(isset($this->html)) { return $this->html; }}
 
}
 
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.