Skip to content

Commit

Permalink
add response header find key method. (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar authored and amouhzi committed May 2, 2018
1 parent 16e8489 commit d4970cf
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/Curl/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Curl
/**
* @var string The user agent name which is set when making a request
*/
const USER_AGENT = 'PHP Curl/1.6 (+https://github.com/php-mod/curl)';
const USER_AGENT = 'PHP Curl/1.9 (+https://github.com/php-mod/curl)';

private $_cookies = array();

Expand Down Expand Up @@ -639,4 +639,48 @@ public function isServerError()
{
return $this->http_status_code >= 500 && $this->http_status_code < 600;
}

/**
* Get a specific response header key or all values from the response headers array.
*
* Usage example:
*
* ```php
* $curl = (new Curl())->get('http://example.com');
*
* echo $curl->getResponseHeaders('Content-Type');
* ```
*
* Or in order to dump all keys with the given values use:
*
* ```php
* $curl = (new Curl())->get('http://example.com');
*
* var_dump($curl->getResponseHeaders());
* ```
*
* @param string $headerKey Optional key to get from the array.
* @return boolean|string
* @since 1.9
*/
public function getResponseHeaders($headerKey = null)
{
$headers = [];
$headerKey = strtolower($headerKey);

foreach ($this->response_headers as $header) {
$parts = explode(":", $header, 2);

$key = isset($parts[0]) ? $parts[0] : null;
$value = isset($parts[1]) ? $parts[1] : null;

$headers[trim(strtolower($key))] = trim($value);
}

if ($headerKey) {
return isset($headers[$headerKey]) ? $headers[$headerKey] : false;
}

return $headers;
}
}

0 comments on commit d4970cf

Please sign in to comment.