如何在Guzzle〜6.0中读取响应有效URL [英] How to read the response effective URL in Guzzle ~6.0

查看:201
本文介绍了如何在Guzzle〜6.0中读取响应有效URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了大约2个小时,但无法弄清楚如何读取最终响应uri.

I've been searching for about 2 hours and I can't figure it out how to read the final response uri.

在早期版本的PHP Guzzle 中,您只需调用$response->getEffectiveUrl()即可.

In previous versions of PHP Guzzle you just call $response->getEffectiveUrl() and you get it.

我希望新版本中有类似的东西,所以最终代码如下:

I expected to have something similar in the new version so the final code looks like this:

$response = $httpClient->post('http://service.com/login', [
    'form_params' => [
        'user'   => $user,
        'padss'  => $pass,
    ]
]);

$url = $response->getEffectiveUrl();

但是在最新版本中,$response现在是GuzzleHttp\Psr7\Response,并且没有允许我检索uri的方法.

But in the latest version $response is now a GuzzleHttp\Psr7\Response and there is no method which allow me to retrieve the uri.

我在此处了解了重定向( http://guzzle.readthedocs.org /en/latest/quickstart.html#redirects ),但它什么也没说

I read about the redirects here (http://guzzle.readthedocs.org/en/latest/quickstart.html#redirects) but it says nothing about

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

感谢@YauheniPrakopchyk

Thanks @YauheniPrakopchyk

推荐答案

docs 开始的Guzzle 6.1解决方案

Guzzle 6.1 solution right from the 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

这篇关于如何在Guzzle〜6.0中读取响应有效URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆