来自 GuzzleHttp 响应的 URI

URI from Response on GuzzleHttp

我需要从 GuzzleHTTPResponse 中获取 URI,目前正在使用 getAsync,并同时处理至少 50 个项目,并且需要一种方法来获取我从 guzzle Client.

使用的 URI
$groups->each(function($group) {
    $promises = $group->map( function($lead, $index) {
        $client = new Client(['http_errors' => false]);
        return $client->getAsync($lead->website, [
            'timeout' => 5, // Response timeout
            'connect_timeout' => 5, // Connection timeout
        ]); 
    })->toArray();
    settle($promises)->then( function($results) {
        $collections = collect($results);
        $fulfilled = $collections->where('state', 'fulfilled')->all();
    })->wait();
});

似乎 Request 有这个 getUri 方法,但是 Response 没有也不能在界面或 class 和文档中找到., 希望有人能帮忙

编辑:试过 getEffectiveUrl 但这只适用于 Guzzle 5,目前使用 6

这是给guzzle 5的

在响应中你没有 getUri 方法,因为只有请求有这个。

如果有重定向或发生某些事情,您可以使用以下方法获取响应 url

$response = GuzzleHttp\get('http://httpbin.org/get');
echo $response->getEffectiveUrl();
// http://httpbin.org/get

$response = GuzzleHttp\get('http://httpbin.org/redirect-to?url=http://www.google.com');
echo $response->getEffectiveUrl();
// http://www.google.com

https://docs.guzzlephp.org/en/5.3/http-messages.html#effective-url

狂风 6

Guzzle 6.1 解决方案直接来自 docs

use GuzzleHttp\Client;
use GuzzleHttp\TransferStats;

$client = new Client;

$client->get('http://some.site.com', [
    'query'   => ['get' => 'params'],
    'on_stats' => function (TransferStats $stats) use (&$url) {
        $url = $stats->getEffectiveUri();
    }
])->getBody()->getContents();

echo $url; // http://some.site.com?get=params

感谢