Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the response effective URL in Guzzle ~6.0

Tags:

php

guzzle

psr-7

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

like image 788
joserobleda Avatar asked Jun 06 '15 11:06

joserobleda


People also ask

How do I get my guzzle response code?

You can check to see if a request or response has a body using the getBody() method: $response = GuzzleHttp\get('http://httpbin.org/get'); if ($response->getBody()) { echo $response->getBody(); // JSON string: { ... } }

What is guzzle HTTP client?

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...

How do you set headers in guzzle?

For setting default headers to a Guzzle client (if using the client as a base for multiple requests), its best to set up a middleware which would add the header on every request. Otherwise additional request headers will get overridden in new client requests.

Does guzzle use cURL?

Guzzle has historically only utilized cURL to send HTTP requests. cURL is an amazing HTTP client (arguably the best), and Guzzle will continue to use it by default when it is available. It is rare, but some developers don't have cURL installed on their systems or run into version specific issues.


2 Answers

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 
like image 109
Yauheni Prakopchyk Avatar answered Sep 28 '22 01:09

Yauheni Prakopchyk


You can check what redirects your request had byt setting track_redirects parameter:

$client = new \GuzzleHttp\Client(['allow_redirects' => ['track_redirects' => true]]);  $response = $client->request('GET', 'http://example.com');  var_dump($response->getHeader(\GuzzleHttp\RedirectMiddleware::HISTORY_HEADER)); 

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.

like image 37
bgaluszka Avatar answered Sep 28 '22 03:09

bgaluszka