from_consumer_and_token() public static method

pretty much a helper function to set up the request
public static from_consumer_and_token ( $consumer, $token, $http_method, $http_url, $parameters = NULL )
Ejemplo n.º 1
0
 /**
  * Processing HTTP request.
  * @param  string  URL or twitter command
  * @param  string  HTTP method GET or POST
  * @return stdClass|stdClass[]
  * @throws TwitterException
  */
 public function request($resource, $method, array $data = NULL, array $files = NULL)
 {
     if (!strpos($resource, '://')) {
         if (!strpos($resource, '.')) {
             $resource .= '.json';
         }
         $resource = self::TWITTER_API_URL . $resource;
     }
     $hasCURLFile = class_exists('CURLFile', FALSE) && defined('CURLOPT_SAFE_UPLOAD');
     $request = Twitter_OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $resource, $files ? array() : $data);
     $request->sign_request(new Twitter_OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, $this->token);
     $options = array(CURLOPT_HEADER => FALSE, CURLOPT_RETURNTRANSFER => TRUE) + ($method === 'POST' ? array($hasCURLFile ? CURLOPT_SAFE_UPLOAD : -1 => TRUE, CURLOPT_POST => FALSE, CURLOPT_POSTFIELDS => $files ? $data : $request->to_postdata(), CURLOPT_URL => $files ? $request->to_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));
     }
     $response = defined('JSON_BIGINT_AS_STRING') ? @json_decode($result, FALSE, 128, JSON_BIGINT_AS_STRING) : @json_decode($result);
     if ($response === FALSE) {
         throw new TwitterException('Invalid server response');
     }
     $errno = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     if ($errno >= 400) {
         throw new TwitterException(isset($payload->errors[0]->message) ? $payload->errors[0]->message : "Server error #{$errno}", $errno);
     }
     return $response;
 }
 /**
  * Process HTTP request.
  * @param  string  URL or twitter command
  * @param  string  HTTP method GET or POST
  * @param  array   data
  * @return mixed
  * @throws TwitterException
  */
 public function request($resource, $method, array $data = NULL)
 {
     if (!strpos($resource, '://')) {
         if (!strpos($resource, '.')) {
             $resource .= '.json';
         }
         $resource = self::API_URL . $resource;
     }
     foreach (array_keys((array) $data, NULL, TRUE) as $key) {
         unset($data[$key]);
     }
     $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));
     }
     $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);
     if ($code >= 400) {
         throw new TwitterException(isset($payload->errors[0]->message) ? $payload->errors[0]->message : "Server error #{$code}", $code);
     }
     return $payload;
 }
Ejemplo n.º 3
0
 /**
  * Process HTTP request.
  * @param  string  URL or twitter command
  * @param  string  HTTP method GET or POST
  * @param  array   data
  * @param  array   uploaded files
  * @return stdClass|stdClass[]
  * @throws TwitterException
  */
 public function request($resource, $method, array $data = NULL, array $files = NULL)
 {
     if (!strpos($resource, '://')) {
         if (!strpos($resource, '.')) {
             $resource .= '.json';
         }
         $resource = self::API_URL . $resource;
     }
     $hasCURLFile = class_exists('CURLFile', FALSE) && defined('CURLOPT_SAFE_UPLOAD');
     foreach ((array) $data as $key => $val) {
         if ($val === NULL) {
             unset($data[$key]);
         } elseif ($files && !$hasCURLFile && substr($val, 0, 1) === '@') {
             throw new TwitterException('Due to limitation of cURL it is not possible to send message starting with @ and upload file at the same time in PHP < 5.5');
         }
     }
     foreach ((array) $files as $key => $file) {
         if (!is_file($file)) {
             throw new TwitterException("Cannot read the file {$file}. Check if file exists on disk and check its permissions.");
         }
         $data[$key] = $hasCURLFile ? new CURLFile($file) : '@' . $file;
     }
     $request = Twitter_OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $resource, $files ? array() : $data);
     $request->sign_request(new Twitter_OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, $this->token);
     $options = array(CURLOPT_HEADER => FALSE, CURLOPT_RETURNTRANSFER => TRUE) + ($method === 'POST' ? array($hasCURLFile ? CURLOPT_SAFE_UPLOAD : -1 => TRUE, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $files ? $data : $request->to_postdata(), CURLOPT_URL => $files ? $request->to_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));
     }
     $payload = defined('JSON_BIGINT_AS_STRING') ? @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);
     if ($code >= 400) {
         throw new TwitterException(isset($payload->errors[0]->message) ? $payload->errors[0]->message : "Server error #{$code}", $code);
     }
     return $payload;
 }
Ejemplo n.º 4
0
 /**
  * Process HTTP request.
  * @param  string  URL or twitter command
  * @param  array   data
  * @param  string  HTTP method
  * @return mixed
  * @throws TwitterException
  */
 public function request($request, $data = NULL, $method = 'POST')
 {
     if (!strpos($request, '://')) {
         if (!strpos($request, '.')) {
             $request .= '.json';
         }
         $request = self::API_URL . $request;
     }
     $request = Twitter_OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $request, $data);
     $request->sign_request($this->signatureMethod, $this->consumer, $this->token);
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_HEADER, FALSE);
     curl_setopt($curl, CURLOPT_TIMEOUT, 20);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
     // no echo, just return result
     curl_setopt($curl, CURLOPT_USERAGENT, 'Twitter for PHP');
     if ($method === 'POST') {
         curl_setopt($curl, CURLOPT_POST, TRUE);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $request->to_postdata());
         curl_setopt($curl, CURLOPT_URL, $request->get_normalized_http_url());
     } else {
         curl_setopt($curl, CURLOPT_URL, $request->to_url());
     }
     $result = curl_exec($curl);
     if (curl_errno($curl)) {
         throw new TwitterException('Server error: ' . curl_error($curl));
     }
     $type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
     if (strpos($type, 'xml')) {
         $payload = @simplexml_load_string($result);
         // intentionally @
     } elseif (strpos($type, 'json')) {
         $payload = @json_decode($result, $this->jsonArray);
         // intentionally @
     }
     if (!is_array($payload) && empty($payload)) {
         throw new TwitterException('Invalid server response');
     }
     $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     if ($code >= 400) {
         throw new TwitterException(isset($payload->error) ? $payload->error : "Server error #{$code}", $code);
     }
     return $payload;
 }