【发布时间】:2017-02-22 05:43:18
【问题描述】:
我正在使用 Guzzle 并发请求工具: http://docs.guzzlephp.org/en/latest/quickstart.html#concurrent-requests
我的代码和示例代码类似:
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client();
$requests = function ($total) {
$uri = 'http://127.0.0.1:8126/guzzle-server/perf';
for ($i = 0; $i < $total; $i++) {
yield new Request('GET', $uri);
}
};
$pool = new Pool($client, $requests(100), [
'concurrency' => 5,
'fulfilled' => function ($response, $index) {
// this is delivered each successful response
},
'rejected' => function ($reason, $index) {
// this is delivered each failed request
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
问题是我的一些请求返回了 500 个 HTTP 响应的响应,但仍然发送了一些内容(例如,错误发生的原因)。不幸的是,Guzzle 将带有 500 个状态代码的 http 响应归类为“已拒绝”,我似乎无法获得原始响应,因为该参数在被拒绝的函数中不存在。
但是我可以访问$reason。在我的例子中,它包含一个像这样的 JSON:
{
xdebug: "..."
}
xdebug 属性包含 HTML 字符串,如下所示:
GuzzleHttp\Exception\ServerException:服务器错误:`GET http://example.com` 导致“500 内部服务器错误” 响应:{"failure_reason":"Useful message"} in [...stacktrace ...]
虽然这包含原始响应,但我无法轻松将其提取为隐藏在 HTML 中,因此非常无用。我也不知道这是如何设置的。
因此我的问题是,如何访问被拒绝的并发请求的响应?
【问题讨论】: