2
votes

I would like to get the response headers of an url, example:

$client = new \GuzzleHttp\Client();
$response = $client->head('http://example.com/');
echo $response->getStatusCode();

I cannot use HEAD requests because some web server doesn't recognize HEAD requests (sometime they return 403 forbidden or internal server error).

Is there any way to get headers with guzzle without doing HEAD?

Clarification

I want to get just the headers and not the full body response. Imagine i want to check the headers of a large file, if i use get, Guzzle will download all the file and I don't want that

1
I think you might be confusing the HTTP verb HEAD with a request for headers. Any Guzzle response object will allow you to get the headers of the response.benJ

1 Answers

1
votes

Like benJ said, you might be confusing the HTTP verb HEAD with a request for headers. You should use the right verb in this request and then get the headers from the response. See this working example with a GET request:

$client = new \GuzzleHttp\Client();
$response = $client->get('http://example.com/'); // use the method according to the http verb, e.g. POST will be ->post('http://example.com/')
$headers = $response->getHeaders();