I need to get the URI
from the Response
of GuzzleHTTP
, currently using the getAsync
, and processing the at least 50 items at the same time and needed a way to get the URI
that I use from the guzzle Client
.
$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(); });
it seems like the Request
has this getUri
method but the Response
doesn’t and can’t find in the interface or class and in the documentation., hope someone can help
Edit: tried getEffectiveUrl
but this only works on Guzzle 5, currently using 6
Advertisement
Answer
This is for guzzle 5
On the response you dont have a getUri method cause only the request has this.
If there is a redirect or something happening you may use the following method the get the response url
$response = GuzzleHttpget('http://httpbin.org/get'); echo $response->getEffectiveUrl(); // http://httpbin.org/get $response = GuzzleHttpget('http://httpbin.org/redirect-to?url=http://www.google.com'); echo $response->getEffectiveUrl(); // http://www.google.com
https://docs.guzzlephp.org/en/5.3/http-messages.html#effective-url
Guzzle 6
Guzzle 6.1 solution right from the docs.
use GuzzleHttpClient; use GuzzleHttpTransferStats; $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
Credits to