GuzzleHttp上响应的URI



我需要从GuzzleHTTPResponse获得URI,目前正在使用getAsync,同时处理至少50个项目,并且需要一种方法从狂饮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();
});

看起来CCD_ 7有这个CCD_ 8方法,但CCD_。,希望有人能帮助

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

这是为guzzle 5准备的

在响应中,您没有getUri方法,因为只有请求具有此方法。

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

$response = GuzzleHttpget('http://httpbin.org/get');
echo $response->getEffectiveUrl();
// http://httpbin.org/get
$response = GuzzleHttpget('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解决方案来自文档。

use GuzzleHttpClient;
use GuzzleHttpTransferStats;
$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

的信用

https://stackoverflow.com/a/35443523/8193279

最新更新