Beispiel #1
0
 /**
  * Builds the data one would send in a POST request
  *
  * @return string
  */
 public function toPostdata()
 {
     return Util::buildHttpQuery($this->parameters);
 }
Beispiel #2
0
 /**
  * Make an HTTP request
  *
  * @param string $url
  * @param string $method
  * @param string $authorization
  * @param array $postfields
  *
  * @return string
  * @throws TwitterOAuthException
  */
 private function request($url, $method, $authorization, $postfields)
 {
     /* Curl settings */
     $options = [CURLOPT_CAINFO => __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem', CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => ['Accept: application/json', $authorization, 'Expect:'], CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_TIMEOUT => $this->timeout, CURLOPT_URL => $url, CURLOPT_USERAGENT => $this->userAgent, CURLOPT_ENCODING => 'gzip'];
     if (!empty($this->proxy)) {
         $options[CURLOPT_PROXY] = $this->proxy['CURLOPT_PROXY'];
         $options[CURLOPT_PROXYUSERPWD] = $this->proxy['CURLOPT_PROXYUSERPWD'];
         $options[CURLOPT_PROXYPORT] = $this->proxy['CURLOPT_PROXYPORT'];
         $options[CURLOPT_PROXYAUTH] = CURLAUTH_BASIC;
         $options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
     }
     switch ($method) {
         case 'GET':
             break;
         case 'POST':
             $options[CURLOPT_POST] = true;
             $options[CURLOPT_POSTFIELDS] = Util::buildHttpQuery($postfields);
             break;
         case 'DELETE':
             $options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
             break;
         case 'PUT':
             $options[CURLOPT_CUSTOMREQUEST] = 'PUT';
             break;
     }
     if (in_array($method, ['GET', 'PUT', 'DELETE']) && !empty($postfields)) {
         $options[CURLOPT_URL] .= '?' . Util::buildHttpQuery($postfields);
     }
     $curlHandle = curl_init();
     curl_setopt_array($curlHandle, $options);
     $response = curl_exec($curlHandle);
     // Throw exceptions on cURL errors.
     if (curl_errno($curlHandle) > 0) {
         throw new TwitterOAuthException(curl_error($curlHandle), curl_errno($curlHandle));
     }
     $this->response->setHttpCode(curl_getinfo($curlHandle, CURLINFO_HTTP_CODE));
     $parts = explode("\r\n\r\n", $response);
     $responseBody = array_pop($parts);
     $responseHeader = array_pop($parts);
     $this->response->setHeaders($this->parseHeaders($responseHeader));
     curl_close($curlHandle);
     return $responseBody;
 }