Permalink
Browse files

replaced CURL with stream_context (experimental)

  • Loading branch information...
1 parent 5ca5b8c commit 48b66c99be57ce332bd874b9800df7fd8f807fc7 @dg committed Jun 5, 2011
Showing with 20 additions and 26 deletions.
  1. +1 −1 readme.md
  2. +19 −25 src/twitter.class.php
View
@@ -4,7 +4,7 @@
Twitter for PHP is a very small and easy-to-use library for sending
messages to Twitter and receiving status updates.
-It requires PHP 5.0 or newer with CURL extension and is licensed under the New BSD License.
+It requires PHP 5.0 or newer with enabled `allow_url_fopen` and `openssl` and is licensed under the New BSD License.
You can obtain the latest version from our [GitHub repository](http://github.com/dg/twitter-php)
or install it via Composer:
View
@@ -32,10 +32,8 @@ class Twitter
/** @var array */
public $httpOptions = array(
- CURLOPT_TIMEOUT => 20,
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_HTTPHEADER => array('Expect:'),
- CURLOPT_USERAGENT => 'Twitter for PHP',
+ 'timeout' => 20,
+ 'user_agent' => 'Twitter for PHP',
);
/** @var Twitter_OAuthSignatureMethod */
@@ -55,14 +53,13 @@ class Twitter
* @param string app secret
* @param string optional access token
* @param string optinal access token secret
- * @throws TwitterException when CURL extension is not loaded
+ * @throws TwitterException when allow_url_fopen is not enabled
*/
public function __construct($consumerKey, $consumerSecret, $accessToken = NULL, $accessTokenSecret = NULL)
{
- if (!extension_loaded('curl')) {
- throw new TwitterException('PHP extension CURL is not loaded.');
+ if (!ini_get('allow_url_fopen') || !extension_loaded('openssl')) {
+ throw new TwitterException('Class Twitter requires that directive allow_url_fopen and extension openssl are enabled.');
}
-
$this->signatureMethod = new Twitter_OAuthSignatureMethod_HMAC_SHA1();
$this->consumer = new Twitter_OAuthConsumer($consumerKey, $consumerSecret);
$this->token = new Twitter_OAuthConsumer($accessToken, $accessTokenSecret);
@@ -208,32 +205,29 @@ public function request($resource, $method, array $data = NULL)
$request = Twitter_OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $resource, $data);
$request->sign_request($this->signatureMethod, $this->consumer, $this->token);
- $options = array(
- CURLOPT_HEADER => FALSE,
- CURLOPT_RETURNTRANSFER => TRUE,
- ) + ($method === 'POST' ? array(
- CURLOPT_POST => TRUE,
- CURLOPT_POSTFIELDS => $request->to_postdata(),
- CURLOPT_URL => $request->get_normalized_http_url(),
- ) : array(
- CURLOPT_URL => $request->to_url(),
- )) + $this->httpOptions;
-
- $curl = curl_init();
- curl_setopt_array($curl, $options);
- $result = curl_exec($curl);
- if (curl_errno($curl)) {
- throw new TwitterException('Server error: ' . curl_error($curl));
+ $context = stream_context_create(array(
+ 'http' => array(
+ 'method' => $method,
+ 'content' => $method === 'POST' ? $request->to_postdata() : NULL,
+ ) + $this->httpOptions,
+ ));
+
+ $f = @fopen($method === 'POST' ? $request->get_normalized_http_url() : $request->to_url(), 'r', FALSE, $context);
+ if (!$f) {
+ $err = error_get_last();
+ throw new TwitterException('Server error' . substr(strstr($err['message'], ')'), 1));
}
+ $result = stream_get_contents($f);
$payload = version_compare(PHP_VERSION, '5.4.0') >= 0 ?
@json_decode($result, FALSE, 128, JSON_BIGINT_AS_STRING) : @json_decode($result); // intentionally @
if ($payload === FALSE) {
throw new TwitterException('Invalid server response');
}
- $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
+ $meta = stream_get_meta_data($f);
+ $code = preg_match('~^HTTP/[\d.]+ (\d+)~m', implode("\n", array_reverse($meta['wrapper_data'])), $m) ? (int) $m[1] : NULL;
if ($code >= 400) {
throw new TwitterException(isset($payload->errors[0]->message) ? $payload->errors[0]->message : "Server error #$code", $code);
}

0 comments on commit 48b66c9

Please sign in to comment.