Recently while writing some code that utilised the SlideShare API I found myself on a server which had url_file_open turned off in the php.ini and this couldn’t be over-rode with a .htaccess file either. So I had to re-write the code which used file_get_contents using the cURL library.
I needed to replace the get_data function within the SSUtil.php file (part of the SlideShare PHP kit). Initially it looked like this:
private function get_data($call,$params) { $ts=time(); $hash=sha1($this->secret.$ts); try { $res=file_get_contents($this->apiurl.$call."?api_key=$this->key&ts=$ts&hash=$hash".$params); } catch (Exception $e) { // Log the exception and return $res as blank } return utf8_encode($res); }
Following some time spent with google and a bit or trial-and-error I came up with this which seems to work in its place:
private function get_data($call,$params) { $timeout=0; $ts=time(); $hash=sha1($this->secret.$ts); # use CURL library to fetch remote file try { $ch = curl_init(); $url = $this->apiurl.$call."?api_key=$this->key&ts=$ts&hash=$hash".$params; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $this->file_contents = curl_exec($ch); $res = $this->file_contents; } catch (Exception $e) { // Log the exception and return $res as blank } return utf8_encode($res); }
So, for any other unlucky souls who find themselves on a server which doesn’t support url_file_open then there you are.
SlideShare API without url_file_open was posted on 24 January, 2009 at 9:36 pm in Coding & Projects and tagged as hack, how-to, php, slideshare. It was last modified on January 29, 2011 at 8:08 pm. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response or trackback from your site.




Jamie Davies is occasionally brilliant, sometimes moody, workaholic and lazy to the bone. A 29-year-old psychology teacher, writer, geek and all-round-top-chap from Yorkshire.