Skip to content

Commit

Permalink
Initial commit of the HTTP Streams modification to the Facebook PHP SDK.
Browse files Browse the repository at this point in the history
  • Loading branch information
camfitz committed Jan 30, 2013
1 parent bf99924 commit 2a392e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 55 deletions.
6 changes: 6 additions & 0 deletions 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
Expand Down
74 changes: 19 additions & 55 deletions src/base_facebook.php
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 2a392e0

Please sign in to comment.