使用 Guzzle Http 客户端获取原始 headers

Getting raw headers with Guzzle Http Client

我有 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() );

假设我用本机 PHP 函数调用此 URL get_headers:

var_dump( get_headers( 'http://forexample.com/index' ) );

我得到了所有 header。那么有什么想法吗?

Guzzle 具有各种代码的预定义状态消息。 See here.

因此,您的消息将根据发送的代码被替换为该消息。并且可以通过

获取默认消息
$res->getReasonPhrase();

更新

我知道为什么我们没有得到 "Some message" 和 $res->getReasonPhrase(); 函数。问题(?)位于第 76 行 here

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 铸造背后一定有一些原因,但我通过将 int 替换为 string 创建了一个 pull request 只是想知道是否有任何背后的原因。我希望他们拒绝我的拉取请求并告诉我为什么我错了并且 int 铸造确实是正确的。

更新

拉取请求已被接受并合并到 master 中。所以在未来的版本中这个错误会被修复。