简体   繁体   English

从 Guzzle 响应消息中获取 http 请求标头作为字符串

[英]Get http Request headers from Guzzle Response Message as string

After doing an http Request by using Guzzle, I want to print all the response headers.使用 Guzzle 执行 http 请求后,我想打印所有响应标头。 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在 guzzle 文档中指出getHeaders()方法应该能够将标头转换为字符串,但是

<?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.还指出在GuzzleHttp\\Message\\Response中应该有一个名为getRawHeaders()的方法,它应该将标题作为字符串返回,但是 php 告诉我该方法在 Response 对象上未定义。 So, how can I accomplish my task of printing all the response headers as a string?那么,如何完成将所有响应标头打印为字符串的任务?

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.如果您想在 Guzzle 6.0 中查看详细版本的响应和请求标头,则需要在请求中启用调试选项。 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.查看 文档页面,您可以在其中找到更多信息。

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:根据 api ( http://api.guzzlephp.org/class-Guzzle.Http.Message.Response.html#_getRawHeaders ),你可以这样做:

echo $response->getRawHeaders();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM