为了账号安全,请及时绑定邮箱和手机立即绑定

来自 GuzzleHttp 响应的 URI

来自 GuzzleHttp 响应的 URI

PHP
陪伴而非守候 2023-09-15 21:44:34
URI我需要从当前使用的Response中获取,并同时处理至少 50 个项目,并且需要一种方法来从 guzzle 中获取我使用的。GuzzleHTTPgetAsyncURIClient$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在接口或类和文档中没有也找不到。希望有人可以提供帮助编辑:尝试过getEffectiveUrl,但这仅适用于 Guzzle 5,目前使用 6
查看完整描述

1 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

这是给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


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

查看完整回答
反对 回复 2023-09-15
  • 1 回答
  • 0 关注
  • 70 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信