How to read the response effective URL in Guzzle ~6.0

joserobleda picture joserobleda · Jun 6, 2015 · Viewed 34.8k times · Source

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

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();

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

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

UPDATE: The 6.1 version now allows you to easily do this:

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

Thanks @YauheniPrakopchyk

Answer

Yauheni Prakopchyk picture Yauheni Prakopchyk · Feb 16, 2016

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