/ Published in: PHP
Function to use the oembed services in php
Expand |
Embed | Plain Text
// Curl helper function function curl_get($url) { $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 30); $return = curl_exec($curl); curl_close($curl); return $return; } // Usage // todo better handling and detection of the service and adding some more... // getOEmbed("any service url"); -> This return an information array of the media // getOEmbed("any service url", true); -> This return the html for the media // getOEmbed("any service url", array("maxwidth"=>300)); -> This return an information array of the media with the parameter // getOEmbed("any service url", array("maxwidth"=>300), true); -> This return the html for the media with the parameter function getOEmbed($url=null, $params=null, $returnHtml=null) { if($url == null) return; $oEmbedUrl = null; "vimeo.com" => "http://www.vimeo.com/api/oembed.json?url=", "youtube.com" => "http://www.youtube.com/oembed?url=", "flickr.com/photos" => "http://www.flickr.com/services/oembed/?url=" ); // Try to find the services based on the domain name with the one in the services array foreach($services as $servicesName => $servicesUrl) { $oEmbedUrl = $servicesUrl.$url; } } // Check for the oembed link embeded exists in the page if($oEmbedUrl == null) { $data = curl_get($url); $pattern = "/type=\"application\/json\+oembed\" href=[\\\"']?(.[^\\\"']*)[\\\"']?/i"; } // If found nothing at that point, use the oohembed.com service. if($oEmbedUrl == null) { $oEmbedUrl = "http://oohembed.com/oohembed/?url=".$url; } $param = ""; foreach($params as $key => $value) $param .= "&".$key."=".$value; $ret = json_decode(curl_get($oEmbedUrl.$param."&format=json")); if($returnHtml) { if($ret->type == "video") { return $ret->html; } else { return "<img src=\"".$ret->url."\" width=\"".$ret->width."\" height=\"".$ret->height."\" />"; } } else { return $ret; } }
You need to login to post a comment.
