From 2a392e0409c1ae73bc410f60ea8cbd0fffc3a981 Mon Sep 17 00:00:00 2001 From: Cameron Fitzgerald Date: Wed, 30 Jan 2013 14:49:23 +1100 Subject: [PATCH] Initial commit of the HTTP Streams modification to the Facebook PHP SDK. --- readme.md | 6 ++++ src/base_facebook.php | 74 +++++++++++-------------------------------- 2 files changed, 25 insertions(+), 55 deletions(-) diff --git a/readme.md b/readme.md index ebbe667e..9ebf25e9 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,9 @@ +Fork of the Facebook PHP SDK +----- +This fork modifies the Facebook PHP SDK to run with HTTP Streams rather than +Curl, allowing it to operate on systems without the Curl extension installed. + + Facebook PHP SDK (v.3.2.2) The [Facebook Platform](http://developers.facebook.com/) is diff --git a/src/base_facebook.php b/src/base_facebook.php index 2ea0fb43..5a3a4702 100644 --- a/src/base_facebook.php +++ b/src/base_facebook.php @@ -15,9 +15,6 @@ * under the License. */ -if (!function_exists('curl_init')) { - throw new Exception('Facebook needs the CURL PHP extension.'); -} if (!function_exists('json_decode')) { throw new Exception('Facebook needs the JSON PHP extension.'); } @@ -921,10 +918,6 @@ protected function _oauthRequest($url, $params) { * @return string The response text */ protected function makeRequest($url, $params, $ch=null) { - if (!$ch) { - $ch = curl_init(); - } - $opts = self::$CURL_OPTS; if ($this->getFileUploadSupport()) { $opts[CURLOPT_POSTFIELDS] = $params; @@ -933,58 +926,29 @@ protected function makeRequest($url, $params, $ch=null) { } $opts[CURLOPT_URL] = $url; - // disable the 'Expect: 100-continue' behaviour. This causes CURL to wait - // for 2 seconds if the server does not support this header. - if (isset($opts[CURLOPT_HTTPHEADER])) { - $existing_headers = $opts[CURLOPT_HTTPHEADER]; - $existing_headers[] = 'Expect:'; - $opts[CURLOPT_HTTPHEADER] = $existing_headers; - } else { - $opts[CURLOPT_HTTPHEADER] = array('Expect:'); + $method = 'GET'; + if(!isset($params['method']) || $params['method'] == '') { + $method = 'GET'; } - - curl_setopt_array($ch, $opts); - $result = curl_exec($ch); - - if (curl_errno($ch) == 60) { // CURLE_SSL_CACERT - self::errorLog('Invalid or no certificate authority found, '. - 'using bundled information'); - curl_setopt($ch, CURLOPT_CAINFO, - dirname(__FILE__) . '/fb_ca_chain_bundle.crt'); - $result = curl_exec($ch); + else { + $method = $params['method']; } - // With dual stacked DNS responses, it's possible for a server to - // have IPv6 enabled but not have IPv6 connectivity. If this is - // the case, curl will try IPv4 first and if that fails, then it will - // fall back to IPv6 and the error EHOSTUNREACH is returned by the - // operating system. - if ($result === false && empty($opts[CURLOPT_IPRESOLVE])) { - $matches = array(); - $regex = '/Failed to connect to ([^:].*): Network is unreachable/'; - if (preg_match($regex, curl_error($ch), $matches)) { - if (strlen(@inet_pton($matches[1])) === 16) { - self::errorLog('Invalid IPv6 configuration on server, '. - 'Please disable or get native IPv6 on your server.'); - self::$CURL_OPTS[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4; - curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); - $result = curl_exec($ch); - } - } - } + // Convert to Stream Options + $stream_opts = array( + 'http'=>array( + 'method'=>$method, + 'user-agent'=>$opts['CURLOPT_USERAGENT'], + 'timeout'=>floatval($opts['CURLOPT_TIMEOUT']), + ), + 'ssl'=>array( + 'verify_peer'=>'true' + ) + ); - if ($result === false) { - $e = new FacebookApiException(array( - 'error_code' => curl_errno($ch), - 'error' => array( - 'message' => curl_error($ch), - 'type' => 'CurlException', - ), - )); - curl_close($ch); - throw $e; - } - curl_close($ch); + $context = stream_context_create($stream_opts); + $url = $opts['CURLOPT_URL'] . '?' . $opts['CURLOPT_POSTFIELDS']; + $result = file_get_contents($url, false, $context); return $result; }