Created
March 19, 2014 00:06
-
-
Save toddbc/9632844 to your computer and use it in GitHub Desktop.
Wrapper for Facebook class without using CURL.
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 | |
// If these aren't defined, define them so the official API client doesn't cry. | |
if (!defined('CURLOPT_USERAGENT')) | |
define('CURLOPT_USERAGENT', 'CURLOPT_USERAGENT'); | |
if (!defined('CURLOPT_CONNECTTIMEOUT')) | |
define('CURLOPT_CONNECTTIMEOUT', 'CURLOPT_CONNECTTIMEOUT'); | |
if (!defined('CURLOPT_TIMEOUT')) | |
define('CURLOPT_TIMEOUT', 'CURLOPT_TIMEOUT'); | |
if (!defined('CURLOPT_RETURNTRANSFER')) | |
define('CURLOPT_RETURNTRANSFER', 'CURLOPT_RETURNTRANSFER'); | |
// We create this so that Facebook's official API client doesn't die with an error. | |
if (!function_exists('curl_init')) { | |
function curl_init() | |
{ | |
throw new Exception('Unimplemented.'); | |
} | |
} | |
// Now we are prepared for the official one. | |
require(dirname(__FILE__) . '/facebook.php'); | |
class FacebookNoCurl extends Facebook | |
{ | |
/** | |
* Makes an HTTP request. Overriden to use something other than curl. | |
* | |
* @param string $url the URL to make the request to | |
* @param array $params the parameters to use for the POST body | |
* @param null $ch not supported | |
* @return string the response text | |
*/ | |
protected function makeRequest($url, $params, $ch = null) | |
{ | |
if ($ch !== null) | |
throw new FacebookApiException(array( | |
'error_code' => 2, | |
'error' => array( | |
'message' => 'CURL support not found within PHP.', | |
'type' => 'CurlException', | |
), | |
)); | |
return $this->makeRequestWithPHP($url, $params); | |
} | |
protected function _exceptionError($errno, $errstr, $errfile, $errline) | |
{ | |
if ($errno & error_reporting()) | |
throw new ErrorException($errstr, 0, $errno, $errfile, $errline); | |
} | |
protected function makeRequestWithPHP($url, $params) | |
{ | |
// If we can't connect and fsockopen() issues an error, throw it. | |
$old_handler = set_error_handler(array($this, '_exceptionError'), E_ALL); | |
// Check for new method added to class recently. | |
if (method_exists($this, 'useFileUploadSupport') && $this->useFileUploadSupport()) | |
throw new FacebookApiException(array( | |
'error_code' => 2, | |
'error' => array( | |
'message' => '@-syntax file upload support is not supported.', | |
'type' => 'CurlException' | |
), | |
)); | |
else | |
$content = http_build_query($params, null, '&'); | |
$context = array( | |
'http' => array( | |
'method' => 'POST', | |
'header' => | |
'Connection: close' . "\r\n" . | |
'Content-Length: ' . strlen($content) . "\r\n" . | |
'Content-Type: application/x-www-form-urlencoded' . "\r\n", | |
'user_agent' => Facebook::$CURL_OPTS[CURLOPT_USERAGENT] . ' php/' . PHP_VERSION, | |
'content' => $content, | |
'protocol_version' => '1.0', | |
'ignore_errors' => true, | |
'timeout' => Facebook::$CURL_OPTS[CURLOPT_TIMEOUT], | |
), | |
'ssl' => array( | |
), | |
); | |
try | |
{ | |
$e = null; | |
$context = stream_context_create($context); | |
$result = file_get_contents($url, false, $context); | |
} | |
catch (ErrorException $e) | |
{ | |
$e = new FacebookApiException(array( | |
'error_code' => $e->getCode(), | |
'error' => array( | |
'message' => $e->getMessage(), | |
'type' => get_class($e), | |
), | |
)); | |
} | |
restore_error_handler($old_handler); | |
if ($e !== null) | |
throw $e; | |
return $result; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment