Skip to content

Instantly share code, notes, and snippets.

@adactio
Created March 24, 2020 11:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adactio/9a0eefab473ce9e98d0bf8e39a807092 to your computer and use it in GitHub Desktop.
Save adactio/9a0eefab473ce9e98d0bf8e39a807092 to your computer and use it in GitHub Desktop.
A PHP function to return an oEmbed result from a URL.
<?php
function getEmbedCode($url="") {
$return = '';
$providers = array(
'flickr.com' => 'https://www.flickr.com/services/oembed/',
'huffduffer.com' => 'https://huffduffer.com/oembed',
'instagram.com' => 'https://api.instagram.com/publicapi/oembed',
'kickstarter.com' => 'https://www.kickstarter.com/services/oembed',
'soundcloud.com' => 'https://soundcloud.com/oembed',
'speakerdeck.com' => 'https://speakerdeck.com/oembed.json',
'ted.com' => 'https://www.ted.com/talks/oembed.json',
'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;
break;
}
}
if (!$endpoint) {
return $return;
}
$options = array(
CURLOPT_URL => $endpoint.'?url='.urlencode($url).'&format=json&scheme=https',
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['html'])) {
$find = '<iframe';
if (isset($response['title'])) {
$replace = '<iframe loading="lazy" title="'.$response['title'].'"';
} else {
$replace = '<iframe loading="lazy" title="'.$domain.'"';
}
$response['html']=str_replace($find,$replace,$response['html']);
}
if (isset($response['type'])) {
switch ($response['type']) {
case 'photo':
if (isset($response['url']) && isset($response['title'])) {
$return = '<img src="'.$response['url'].'" alt="'.$response['title'].'" />';
}
break;
default:
$return = $response['html'];
break;
}
}
if ($response['provider_url'] == 'https://www.youtube.com/') {
parse_str(parse_url($url, PHP_URL_QUERY), $arguments);
$id = $arguments['v'];
if (isset($id)) {
$return = '<div>';
$return.= '<a class="videoimglink" href="'.$url.'" onclick="event.preventDefault();this.parentNode.innerHTML=\'';
$return.= '<iframe src=https://www.youtube-nocookie.com/embed/'.$id.'?autoplay=1></iframe>';
$return.= '\';">';
$return.= '<img width="100%" loading="lazy" src="https://i.ytimg.com/vi/'.$id.'/default.jpg" alt="'.$response['title'].'" srcset="';
$return.= 'https://i.ytimg.com/vi/'.$id.'/mqdefault.jpg 320w,';
$return.= 'https://i.ytimg.com/vi/'.$id.'/hqdefault.jpg 480w,';
$return.= 'https://i.ytimg.com/vi/'.$id.'/maxresdefault.jpg 1280w';
$return.= '">';
$return.= '</a>';
$return.= '</div>';
}
}
return $return;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment