Last active
November 7, 2015 10:22
-
-
Save adactio/5d7224ea09d75844b677 to your computer and use it in GitHub Desktop.
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 | |
# 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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A function that checks a URL to see if it has an oEmbed endpoint and if it does, returns the embeddable content.