我有 URL ( http://forexample.com/index ),我在其中发送原始 header :
header("HTTP/1.1 401 Some message");
我想使用 Guzzle 获取此原始 header 消息。不幸的是,请求完成后,我无法在 header 之间找到此消息:
$client = new GuzzleHttp\Client();
try {
$res = $client->post( 'http://forexample.com/index' );
} catch ( GuzzleHttp\Exception\BadResponseException $e ) {
// On HTTP response other than 200 Guzzle throws an exception
$res = $e->getResponse();
}
var_dump( $res->getHeaders() );
假设我使用 native PHP 函数 get_headers
调用此 URL:
var_dump( get_headers( 'http://forexample.com/index' ) );
我得到了所有的标题。那么有什么想法吗?
最佳答案
Guzzle 为各种代码预定义了状态消息。 See here .
因此您的消息将根据发送的代码被该消息替换。并且可以通过以下方式获取默认消息,
$res->getReasonPhrase();
更新
我知道为什么我们没有通过 $res->getReasonPhrase();
函数收到“Some message”。问题(?)在于here在第 76 行。
isset($startLine[2]) ? (int) $startLine[2] : null
在上一行中,$startLine[2]
是您在 header 中提供的 'Some message'
,但由于 int
转换,结果为 0
并且由于以下代码 here替换为默认消息。
if (!$reason && isset(self::$phrases[$this->statusCode])) {
$this->reasonPhrase = self::$phrases[$status];
} else {
$this->reasonPhrase = (string) $reason;
}
我确信 int
转换背后一定有一些原因,但我已经创建了一个 pull request通过用 string
替换 int
只是为了知道它背后是否有任何原因。我希望他们拒绝我的拉取请求并告诉我为什么我错了,int
转换确实是正确的。
更新
拉取请求已被接受并合并到 master 中。所以在未来的版本中,这个错误会被修复。
关于php - 使用 Guzzle Http 客户端获取原始 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31518576/