Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guzzle retrieve headers without body, using GET

Tags:

php

guzzle

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

like image 967
giò Avatar asked Jul 05 '26 21:07

giò


1 Answers

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();
like image 161
Guilherme Pressutto Avatar answered Jul 07 '26 10:07

Guilherme Pressutto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!