1

I have just upgraded from guzzle 3 to guzzle 6

Now i have some code here..

$request = $this->_client->get($url);
           $response = $request->send();
           $url = $response->getInfo('url');

           return $url;

After updating to guzzle 6 I see that getInfo() & also geteffectiveurl() has been removed.. for some reason. so my new code is...

$res = $this->_client->request('GET', $url, ['on_stats' => function (TransferStats $stats) use (&$url) {
                $url = $stats->getEffectiveUri();
                }])->getBody()->getContents();

                return $url;

Now that $url variable is a GuzzleHttp\Psr7\Uri Object which doesnt really solve my problem as i just need to return the url as a string.

How can i covert the object ->

[24-Mar-2017 19:12:26 UTC] GuzzleHttp\Psr7\Uri Object
(
    [scheme:GuzzleHttp\Psr7\Uri:private] => https
    [userInfo:GuzzleHttp\Psr7\Uri:private] => 
    [host:GuzzleHttp\Psr7\Uri:private] => signup.testapp.com
    [port:GuzzleHttp\Psr7\Uri:private] => 
    [path:GuzzleHttp\Psr7\Uri:private] => /login
    [query:GuzzleHttp\Psr7\Uri:private] => username=jeff&blablablablabla
    [fragment:GuzzleHttp\Psr7\Uri:private] => 
)

Into a simple string that i can pass on to make another request to?

Or am i missing somthing? getInfo('url') in Guzzle 3 was the perfect solution to a problem surely another one has taken its place?

Thanks

Matthew Harris
  • 51
  • 3
  • 10

1 Answers1

4

\GuzzleHttp\Psr7\Uri has a magic __toString() method which will return you the URI as a string.

If you are just wanting to send another request to the exact same URI the docs state

When creating a request, you can provide the URI as a string or an instance of Psr\Http\Message\UriInterface.

That means your second request to the same URI could be executed with the $url object you have.

// Your existing code
// I assume this is within a method since it returns $url
$res = $this->_client->request('GET', $url, [
    'on_stats' => function (TransferStats $stats) use (&$url) {
        $url = $stats->getEffectiveUri();
    }
])->getBody()->getContents();

return $url;


// Make a second request to the same URI
// This would be in another method or something after receiving the $url from the above method
$response2 = $this->_client->request('GET', $url);
J Cobb
  • 294
  • 3
  • 14
  • Thanks for your reply, i worked out a way around it using GuzzleHttp\TransferStats and making a little method to find the last URL but this is a bit long winded so i will try your suggested answer thank you! – Matthew Harris Mar 25 '17 at 10:02