private function request($httpMethod, $path, array $getParams, $body, array $headers = null, $cookies = null)
{
$uri = $this->buildFullUri($path, $getParams);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_PORT, $this->port);
if ($headers != null) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
if ($cookies) {
curl_setopt($ch, CURLOPT_COOKIE, $cookies);
}
if (isset($httpMethod)) {
// Hey, guess what? Curl option PUT won't work!
// http://stackoverflow.com/questions/5043525/php-curl-http-put
if ($httpMethod == CURLOPT_PUT) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
} elseif ($httpMethod == static::CURLOPT_DELETE) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
} else {
curl_setopt($ch, $httpMethod, true);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
// Do not output the contents of the call, instead return in a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// Set all user defined options last
curl_setopt_array($ch, $this->opts);
$returnContent = curl_exec($ch);
$info = curl_getinfo($ch);
if (curl_errno($ch) != 0 || $returnContent === FALSE) {
$error = curl_error($ch);
curl_close($ch);
throw new \Exception("Error requesting {$uri} with port {$this->port}, curl error: {$error}");
}
curl_close($ch);
$responseArray = array('info' => $info);
if (array_key_exists(CURLOPT_HEADER, $this->opts) && $this->opts[CURLOPT_HEADER]) {
// Split the headers and the body out of the return
// this is the most consistently accepted method I've been able
// to find. still feels janky =|
$pieces = explode("\r\n\r\n", $returnContent);
while ($pieces[0] == 'HTTP/1.1 100 Continue') {
// Just keep popping these off
// If you want to do something more useful with these,
// ...please submit a pull request =/
array_shift($pieces);
}
$headerCount = count($pieces);
if ($headerCount != 2) {
throw new \Exception("Curl got more or less headers ({$headerCount}) than it knows how to deal with");
}
// grab the headers section
$responseArray['headers'] = $this->parseHeaders(array_shift($pieces));
//combine the rest of the pieces, there could be line breaks in the body
$responseArray['content'] = implode("\r\n\r\n", $pieces);
} else {
$responseArray['content'] = $returnContent;
}
return $responseArray;
}