php - 使用 Guzzle Http 客户端获取原始 header

标签 php guzzle

我有 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/

相关文章:

php - 这个 JavaScript 函数到底是做什么的?我如何捕获 select 中的值并将其插入回数据库?

php - 如何使用 Guzzle 修改 curl 的超时时间?

php - 如何使用 Kubernetes API 处理流响应?

javascript - 滚动时新的 ajax 请求

php arrays in array sum value by specific key?

php - guzzle NULL响应lumen php oauth2

javascript - 如何在 laravel TableView Blade 中显示 JSON 中的指定对象值

php - Laravel 作为客户端时,用于身份验证的 JWT 存储在哪里?

php - 将csv文件加载到mysql后时间戳为空

php - 用种子随机化 PHP 数组?