Skip to content

Instantly share code, notes, and snippets.

@adactio
Last active Nov 7, 2015
Embed
What would you like to do?
<?php
# Licensed under a CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
# http://creativecommons.org/publicdomain/zero/1.0/
function getEmbedCode($url="",$maxwidth=320) {
$return = '';
$providers = array(
'dribbble.com' => 'https://api.embed.ly/v1/api/oembed',
'flickr.com' => 'https://www.flickr.com/services/oembed/',
'huffduffer.com' => 'https://huffduffer.com/oembed',
'instagram.com' => 'https://api.instagram.com/oembed',
'kickstarter.com' => 'https://www.kickstarter.com/services/oembed',
'slideshare.net' => 'http://www.slideshare.net/api/oembed/2',
'soundcloud.com' => 'https://soundcloud.com/oembed',
'speakerdeck.com' => 'https://speakerdeck.com/oembed.json',
'ted.com' => 'https://www.ted.com/talks/oembed.json',
'ustream.tv' => 'http://www.ustream.tv/oembed',
'vimeo.com' => 'https://vimeo.com/api/oembed.json',
'youtube.com' => 'https://www.youtube.com/oembed'
);
$endpoint = false;
foreach ($providers as $domain => $provider) {
if (stristr($url, $domain)) {
$endpoint = $provider;
}
}
if (!$endpoint) {
return $return;
}
$options = array(
CURLOPT_URL => $endpoint.'?url='.urlencode($url).'&format=json&maxwidth='.$maxwidth,
CURLOPT_USERAGENT => 'adactio.com',
CURLOPT_TIMEOUT => 5,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE
);
$curl = curl_init();
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result,true);
if (isset($response['type'])) {
switch ($response['type']) {
case 'photo':
$return = '<img src="'.$response['url'].'" alt="'.$response['title'].'" />';
break;
default:
$return = $response['html'];
break;
}
}
return $return;
}
?>
@adactio
Copy link
Author

adactio commented Jun 14, 2014

A function that checks a URL to see if it has an oEmbed endpoint and if it does, returns the embeddable content.

getEmbedCode('http://huffduffer.com/adactio/163894');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment