A PHP function to return an oEmbed result from a URL.
This file contains 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 | |
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