Yaffs site version 1.1
[yaffs-website] / vendor / j7mbo / twitter-api-php / TwitterAPIExchange.php
index 0cd385209329229367479448a37d20cc0a1c0fb8..d6e3a585cb5d29c8c2bb3bc10f9374a0e76b58e5 100755 (executable)
@@ -2,9 +2,9 @@
 
 /**
  * Twitter-API-PHP : Simple PHP wrapper for the v1.1 API
- * 
+ *
  * PHP version 5.3.10
- * 
+ *
  * @category Awesomeness
  * @package  Twitter-API-PHP
  * @author   James Mallison <me@j7mbo.co.uk>
@@ -59,29 +59,37 @@ class TwitterAPIExchange
      */
     public $requestMethod;
 
+    /**
+     * The HTTP status code from the previous request
+     *
+     * @var int
+     */
+    protected $httpStatusCode;
+
     /**
      * Create the API access object. Requires an array of settings::
      * oauth access token, oauth access token secret, consumer key, consumer secret
      * These are all available by creating your own application on dev.twitter.com
      * Requires the cURL library
      *
-     * @throws \Exception When cURL isn't installed or incorrect settings parameters are provided
+     * @throws \RuntimeException When cURL isn't loaded
+     * @throws \InvalidArgumentException When incomplete settings parameters are provided
      *
      * @param array $settings
      */
     public function __construct(array $settings)
     {
-        if (!in_array('curl', get_loaded_extensions())) 
+        if (!function_exists('curl_init'))
         {
-            throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
+            throw new RuntimeException('TwitterAPIExchange requires cURL extension to be loaded, see: http://curl.haxx.se/docs/install.html');
         }
-        
+
         if (!isset($settings['oauth_access_token'])
             || !isset($settings['oauth_access_token_secret'])
             || !isset($settings['consumer_key'])
             || !isset($settings['consumer_secret']))
         {
-            throw new Exception('Make sure you are passing in the correct parameters');
+            throw new InvalidArgumentException('Incomplete settings passed to TwitterAPIExchange');
         }
 
         $this->oauth_access_token = $settings['oauth_access_token'];
@@ -101,11 +109,11 @@ class TwitterAPIExchange
      */
     public function setPostfields(array $array)
     {
-        if (!is_null($this->getGetfield())) 
-        { 
-            throw new Exception('You can only choose get OR post fields.'); 
+        if (!is_null($this->getGetfield()))
+        {
+            throw new Exception('You can only choose get OR post fields (post fields include put).');
         }
-        
+
         if (isset($array['status']) && substr($array['status'], 0, 1) === '@')
         {
             $array['status'] = sprintf("\0%s", $array['status']);
@@ -118,33 +126,34 @@ class TwitterAPIExchange
                 $value = ($value === true) ? 'true' : 'false';
             }
         }
-        
+
         $this->postfields = $array;
-        
+
         // rebuild oAuth
-        if (isset($this->oauth['oauth_signature'])) {
+        if (isset($this->oauth['oauth_signature']))
+        {
             $this->buildOauth($this->url, $this->requestMethod);
         }
 
         return $this;
     }
-    
+
     /**
      * Set getfield string, example: '?screen_name=J7mbo'
-     * 
+     *
      * @param string $string Get key and value pairs as string
      *
      * @throws \Exception
-     * 
+     *
      * @return \TwitterAPIExchange Instance of self for method chaining
      */
     public function setGetfield($string)
     {
-        if (!is_null($this->getPostfields())) 
-        { 
-            throw new Exception('You can only choose get OR post fields.'); 
+        if (!is_null($this->getPostfields()))
+        {
+            throw new Exception('You can only choose get OR post / post fields.');
         }
-        
+
         $getfields = preg_replace('/^\?/', '', explode('&', $string));
         $params = array();
 
@@ -157,31 +166,31 @@ class TwitterAPIExchange
             }
         }
 
-        $this->getfield = '?' . http_build_query($params);
-        
+        $this->getfield = '?' . http_build_query($params, '', '&');
+
         return $this;
     }
-    
+
     /**
      * Get getfield string (simple getter)
-     * 
+     *
      * @return string $this->getfields
      */
     public function getGetfield()
     {
         return $this->getfield;
     }
-    
+
     /**
      * Get postfields array (simple getter)
-     * 
+     *
      * @return array $this->postfields
      */
     public function getPostfields()
     {
         return $this->postfields;
     }
-    
+
     /**
      * Build the Oauth object using params set in construct and additionals
      * passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
@@ -195,16 +204,16 @@ class TwitterAPIExchange
      */
     public function buildOauth($url, $requestMethod)
     {
-        if (!in_array(strtolower($requestMethod), array('post', 'get')))
+        if (!in_array(strtolower($requestMethod), array('post', 'get', 'put', 'delete')))
         {
-            throw new Exception('Request method must be either POST or GET');
+            throw new Exception('Request method must be either POST, GET or PUT or DELETE');
         }
-        
+
         $consumer_key              = $this->consumer_key;
         $consumer_secret           = $this->consumer_secret;
         $oauth_access_token        = $this->oauth_access_token;
         $oauth_access_token_secret = $this->oauth_access_token_secret;
-        
+
         $oauth = array(
             'oauth_consumer_key' => $consumer_key,
             'oauth_nonce' => time(),
@@ -213,9 +222,9 @@ class TwitterAPIExchange
             'oauth_timestamp' => time(),
             'oauth_version' => '1.0'
         );
-        
+
         $getfield = $this->getGetfield();
-        
+
         if (!is_null($getfield))
         {
             $getfields = str_replace('?', '', explode('&', $getfield));
@@ -231,7 +240,7 @@ class TwitterAPIExchange
                 }
             }
         }
-        
+
         $postfields = $this->getPostfields();
 
         if (!is_null($postfields)) {
@@ -244,22 +253,22 @@ class TwitterAPIExchange
         $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
         $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
         $oauth['oauth_signature'] = $oauth_signature;
-        
-        $this->url = $url;
+
+        $this->url           = $url;
         $this->requestMethod = $requestMethod;
-        $this->oauth = $oauth;
-        
+        $this->oauth         = $oauth;
+
         return $this;
     }
-    
+
     /**
      * Perform the actual data retrieval from the API
-     * 
+     *
      * @param boolean $return      If true, returns data. This is left in for backward compatibility reasons
      * @param array   $curlOptions Additional Curl options for this request
      *
      * @throws \Exception
-     * 
+     *
      * @return string json If $return param is true, returns json data.
      */
     public function performRequest($return = true, $curlOptions = array())
@@ -274,17 +283,22 @@ class TwitterAPIExchange
         $getfield = $this->getGetfield();
         $postfields = $this->getPostfields();
 
-        $options = array(
+        if (in_array(strtolower($this->requestMethod), array('put', 'delete')))
+        {
+            $curlOptions[CURLOPT_CUSTOMREQUEST] = $this->requestMethod;
+        }
+
+        $options = $curlOptions + array(
             CURLOPT_HTTPHEADER => $header,
             CURLOPT_HEADER => false,
             CURLOPT_URL => $this->url,
             CURLOPT_RETURNTRANSFER => true,
             CURLOPT_TIMEOUT => 10,
-        ) + $curlOptions;
+        );
 
         if (!is_null($postfields))
         {
-            $options[CURLOPT_POSTFIELDS] = http_build_query($postfields);
+            $options[CURLOPT_POSTFIELDS] = http_build_query($postfields, '', '&');
         }
         else
         {
@@ -298,6 +312,8 @@ class TwitterAPIExchange
         curl_setopt_array($feed, $options);
         $json = curl_exec($feed);
 
+        $this->httpStatusCode = curl_getinfo($feed, CURLINFO_HTTP_CODE);
+
         if (($error = curl_error($feed)) !== '')
         {
             curl_close($feed);
@@ -309,17 +325,17 @@ class TwitterAPIExchange
 
         return $json;
     }
-    
+
     /**
      * Private method to generate the base string used by cURL
-     * 
+     *
      * @param string $baseURI
      * @param string $method
      * @param array  $params
-     * 
+     *
      * @return string Built base string
      */
-    private function buildBaseString($baseURI, $method, $params) 
+    private function buildBaseString($baseURI, $method, $params)
     {
         $return = array();
         ksort($params);
@@ -328,22 +344,22 @@ class TwitterAPIExchange
         {
             $return[] = rawurlencode($key) . '=' . rawurlencode($value);
         }
-        
-        return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return)); 
+
+        return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return));
     }
-    
+
     /**
      * Private method to generate authorization header used by cURL
-     * 
+     *
      * @param array $oauth Array of oauth data generated by buildOauth()
-     * 
+     *
      * @return string $return Header used by cURL for request
-     */    
+     */
     private function buildAuthorizationHeader(array $oauth)
     {
         $return = 'Authorization: OAuth ';
         $values = array();
-        
+
         foreach($oauth as $key => $value)
         {
             if (in_array($key, array('oauth_consumer_key', 'oauth_nonce', 'oauth_signature',
@@ -351,7 +367,7 @@ class TwitterAPIExchange
                 $values[] = "$key=\"" . rawurlencode($value) . "\"";
             }
         }
-        
+
         $return .= implode(', ', $values);
         return $return;
     }
@@ -381,4 +397,14 @@ class TwitterAPIExchange
 
         return $this->buildOauth($url, $method)->performRequest(true, $curlOptions);
     }
+
+    /**
+     * Get the HTTP status code for the previous request
+     *
+     * @return integer
+     */
+    public function getHttpStatusCode()
+    {
+        return $this->httpStatusCode;
+    }
 }