user3075898
user3075898

Reputation: 175

Get http headers from Guzzle Response Message as string

After doing an http Request by using Guzzle, I want to print all the response headers. How can I do that?

In the guzzle documentation it is stated that the getHeaders() method should be able to cast headers to string, but doing

<?php

    print $response->getHeaders();

?>

does not work. It is also stated that in GuzzleHttp\Message\Response there should be a method called getRawHeaders() that should return the headers as a string, but php tells me that the method is undefined on the Response object. So, how can I accomplish my task of printing all the response headers as a string?

Upvotes: 7

Views: 32438

Answers (2)

vahob
vahob

Reputation: 111

If you would like to see a verbose version of response and request headers with Guzzle 6.0, you need to enable the debug option in your request. For example:

$YourGuzzleclient=new Client();
$YourGuzzleclient->request('POST', '{Your url}',
  ['debug'=>true,'otheroptions'=>array()]
);

This option will print all the response and request headers. Check the documentation page where you can find more information.

Upvotes: 8

taxicala
taxicala

Reputation: 21769

I believe you will have to iterate through the headers, try this:

foreach ($response->getHeaders() as $name => $values) {
    echo $name . ': ' . implode(', ', $values) . "\r\n";
}

As per the api (http://api.guzzlephp.org/class-Guzzle.Http.Message.Response.html#_getRawHeaders), you could do:

echo $response->getRawHeaders();

Upvotes: 3

Related Questions