/**
* 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;
}