/**
* @see HTTPRequest::execute()
*/
public function execute($path = '/', $method = HTTPRequestMethod::GET)
{
$targetURL = $this->httpConnection->getURI() . $path;
$hasParameters = count($this->requestParameter) > 0;
$query = $hasParameters ? http_build_query($this->requestParameter) : null;
switch ($method) {
case HTTPRequestMethod::PUT:
case HTTPRequestMethod::POST:
if ($method != HTTPRequestMethod::POST) {
curl_setopt($this->curlResource, CURLOPT_CUSTOMREQUEST, $method);
} else {
curl_setopt($this->curlResource, CURLOPT_POST, 1);
}
if (empty($this->requestBody)) {
curl_setopt($this->curlResource, CURLOPT_POSTFIELDS, $query);
} else {
if ($hasParameters) {
$targetURL .= '?' . $query;
}
curl_setopt($this->curlResource, CURLOPT_POSTFIELDS, $this->requestBody);
}
curl_setopt($this->curlResource, CURLOPT_URL, $targetURL);
break;
case HTTPRequestMethod::DELETE:
case HTTPRequestMethod::HEAD:
case HTTPRequestMethod::OPTIONS:
case HTTPRequestMethod::TRACE:
curl_setopt($this->curlResource, CURLOPT_CUSTOMREQUEST, $method);
case HTTPRequestMethod::GET:
if ($hasParameters) {
$targetURL .= '?' . $query;
}
curl_setopt($this->curlResource, CURLOPT_URL, $targetURL);
break;
default:
throw new UnexpectedValueException('Método desconhecido');
}
$resp = curl_exec($this->curlResource);
$errno = curl_errno($this->curlResource);
$error = curl_error($this->curlResource);
if ($errno != 0) {
throw new RuntimeException($error, $errno);
}
$this->httpResponse = new HTTPResponse();
$this->httpResponse->setRawResponse($resp);
if ($this->httpResponse->hasResponseHeader('Set-Cookie')) {
$cookieManager = $this->httpConnection->getCookieManager();
if ($cookieManager != null) {
$cookieManager->setCookie($this->httpResponse->getHeader('Set-Cookie'), $this->httpConnection->getHostName());
}
}
$statusCode = $this->httpResponse->getStatusCode();
return $statusCode < 400;
}