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
I'm not an expert in the subject but, from what I understand, the effective url is not something that is defined in a general HTTP message. I think is is something related to Curl and since Guzzle can use any HTTP handler to send requests (see here), the information is not necessarily present.
Guzzle 6.1 solution right from the docs.
You can check what redirects your request had byt setting
track_redirects
parameter:If there were any redirects last one should be your effective url otherewise your initial url.
You can read more about
allow_redirects
here http://docs.guzzlephp.org/en/latest/request-options.html#allow-redirects.I am using middleware to track requests in the redirect chain and save the last one. The uri of the last request is what you want.
Try this code:
Result:
Example with class:
I think it's best to use response headers to store this information. I wrote a simple class that saves effective url in
X-GUZZLE-EFFECTIVE-URL
header:Usage:
Accepted answer didn't work for me but led me on the way: