从Guzzle Response Message获取http请求标头为字符串

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

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?

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

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

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

echo $response->getRawHeaders();

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.