3

In Guzzle 3 you can get the resolved URL (without actually opening it) like this:

$client = new Client([
    'base_uri' => 'http://foo.com',
]);

$request = $client->get('bar.html');

echo $request->getUrl();

In Guzzle 6 this is not working anymore. Is there another way to get "http://foo.com/bar.html"?

localheinz
  • 9,179
  • 2
  • 33
  • 44
Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99
  • Note that `$client->get()` returns a response object in version 6. To get the effective URL look at [this answer](http://stackoverflow.com/a/35443523/57091). – robsch Feb 09 '17 at 10:43

2 Answers2

1

You can use the history middleware, works as advertised:

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;

$container = [];

$stack = HandlerStack::create();
$stack->push(Middleware::history($container));

$client = new Client([
    'base_uri' => 'http://foo.com',
    'handler' => $stack,
]);

$response = $client->request('GET', 'bar.html');

/* @var RequestInterface $request */
$request = $container[0]['request'];

echo $request->getUri();

For reference, see http://docs.guzzlephp.org/en/latest/testing.html#history-middleware.

localheinz
  • 9,179
  • 2
  • 33
  • 44
  • 1
    The correct command to retrieve the RequestInterface instance is: `$request = $container[0]['request'];` – ocornu Oct 25 '17 at 18:46
1

It is a bit late, but for the reference.

You can do it with \GuzzleHttp\Psr7\UriResolver::resolve($baseUri, $relUri);

It converts the relative URI into a new URI that is resolved against the base URI.

$baseUri and $relUri are instances of \Psr\Http\Message\UriInterfaceUriInterface.

zstate
  • 1,995
  • 1
  • 18
  • 20