Example #1
0
 /**
  * Make an HTTP request
  *
  * @param $url
  * @param $method
  * @param $headers
  * @param $postfields
  *
  * @return string
  * @throws TwitterOAuthException
  */
 private function request($url, $method, $headers, $postfields)
 {
     /* Curl settings */
     $options = array(CURLOPT_CAINFO => __DIR__ . DIRECTORY_SEPARATOR . 'cacert.pem', CURLOPT_CAPATH => __DIR__, CURLOPT_CONNECTTIMEOUT => $this->connectionTimeout, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => array($headers, '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':
             if (!empty($postfields)) {
                 $options[CURLOPT_URL] .= '?' . Util::buildHttpQuery($postfields);
             }
             break;
         case 'POST':
             $options[CURLOPT_POST] = true;
             $options[CURLOPT_POSTFIELDS] = Util::buildHttpQuery($postfields);
             break;
     }
     $curlHandle = curl_init();
     curl_setopt_array($curlHandle, $options);
     $response = curl_exec($curlHandle);
     $curlErrno = curl_errno($curlHandle);
     switch ($curlErrno) {
         case 28:
             throw new TwitterOAuthException('Request timed out.');
         case 51:
             throw new TwitterOAuthException('The remote servers SSL certificate or SSH md5 fingerprint failed validation.');
         case 56:
             throw new TwitterOAuthException('Response from server failed or was interrupted.');
     }
     $this->lastHttpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
     if (empty($this->proxy)) {
         list($header, $body) = explode("\r\n\r\n", $response, 2);
     } else {
         list(, $header, $body) = explode("\r\n\r\n", $response, 3);
     }
     list($this->lastHttpHeaders, $this->lastXHeaders) = $this->parseHeaders($header);
     $this->lastHttpInfo = curl_getinfo($curlHandle);
     curl_close($curlHandle);
     return $body;
 }
Example #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;
 }
 /**
  * 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)
 {
     /* Changed to WP function wp_remote_get */
     $args = array('timeout' => $this->timeout, 'user-agent' => $this->userAgent, 'headers' => $authorization, 'compress' => 'gzip');
     if (in_array($method, ['GET', 'PUT', 'DELETE']) && !empty($postfields)) {
         $url .= '?' . Util::buildHttpQuery($postfields);
     }
     $response_raw = wp_remote_get($url, $args);
     $response = wp_remote_retrieve_body($response_raw);
     if (!empty($response_raw['response']['code'])) {
         $this->response->setHttpCode($response_raw['response']['code']);
     }
     $parts = explode("\r\n\r\n", $response);
     $responseBody = array_pop($parts);
     $responseHeader = array_pop($parts);
     $this->response->setHeaders($this->parseHeaders($responseHeader));
     return $responseBody;
 }