for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Palmtree\Curl;
class Response
{
/** @var array */
private $headers = [];
/** @var string */
private $body;
/** @var int */
private $statusCode;
public function __construct(string $response = '', int $statusCode = 0)
$this->parse($response);
$this->statusCode = $statusCode;
}
public function getHeaders(): array
return $this->headers;
public function getHeader(string $key): ?string
return $this->headers[$key] ?? null;
public function getBody(): string
return $this->body;
public function getStatusCode(): int
return $this->statusCode;
public function isOk(): bool
return $this->statusCode >= Curl::HTTP_OK_MIN && $this->statusCode <= Curl::HTTP_OK_MAX;
public function is404(): bool
return $this->statusCode === Curl::HTTP_NOT_FOUND;
public function __toString(): string
private function parse(string $response)
$response = \explode("\r\n\r\n", $response);
if (\count($response) > 1) {
// We want the last two parts
$response = \array_slice($response, -2, 2);
list($headers, $body) = $response;
foreach (\explode("\r\n", $headers) as $header) {
$pair = \explode(': ', $header, 2);
if (isset($pair[1])) {
$this->headers[$pair[0]] = $pair[1];
} else {
$body = $response[0];
$this->body = $body;