HTTPResponse::setRawResponse PHP Méthode

setRawResponse() public méthode

public setRawResponse ( string $response, CookieManager $cookieManager = null )
$response string Toda a resposta da requisição
$cookieManager CookieManager
    public function setRawResponse($response, CookieManager $cookieManager = null)
    {
        $parts = explode("\r\n\r\n", $response);
        if (count($parts) == 2) {
            $matches = array();
            $this->responseBody = $parts[1];
            if (preg_match_all("/(HTTP\\/[1-9]\\.[0-9]\\s+(?<statusCode>\\d+)\\s+(?<statusMessage>.*)|(?<headerName>[^:]+)\\s*:\\s*(?<headerValue>.*))\r\n/m", $parts[0], $matches)) {
                foreach ($matches['statusCode'] as $offset => $match) {
                    if (!empty($match)) {
                        $this->statusCode = (int) $match;
                        $this->statusMessage = $matches['statusMessage'][$offset];
                        break;
                    }
                }
                foreach ($matches['headerName'] as $offset => $name) {
                    if (!empty($name)) {
                        $key = strtolower($name);
                        $header = array('name' => $name, 'value' => $matches['headerValue'][$offset]);
                        if (isset($this->responseHeader[$key])) {
                            if (isset($this->responseHeader[$key]['name'])) {
                                $this->responseHeader[$key] = array($this->responseHeader[$key]);
                            }
                            $this->responseHeader[$key][] = $header;
                        } else {
                            $this->responseHeader[$key] = $header;
                        }
                    }
                }
            }
        } else {
            $this->responseBody = $response;
        }
    }

Usage Example

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