GuzzleHttp\Client ignores base path in base_url

I have a sort of oblique answer that might help someone who's predisposed to the same type of typos as I am.

It's base_uri, and that's "uri". Not base_url, as in "url".

The consequence of setting a base_url is similar to the problem described by OP - the request won't build on the configured base.


You're using an absolute path in your requests, so it's overriding the path set in the base URL. Guzzle follows RFC 3986 when combining URLs: https://www.rfc-editor.org/rfc/rfc3986#section-5.2


Here are examples for Guzzle 7 (7.4.1) of what Michael Dowling wrote

$client = new Client(['base_uri' => 'http://my-app.com/api/']);
$response = $client->get('facets'); // no leading slash - append to base
//    http://my-app.com/api/facets

$client = new Client(['base_uri' => 'http://my-app.com/api/']);
$response = $client->get('facets/'); // no leading slash - append to base // ending slash preserved
//    http://my-app.com/api/facets/

$client = new Client(['base_uri' => 'http://my-app.com/api/']);
$response = $client->get('/facets'); // leading slash - absolute path - base path is lost
//    http://my-app.com/facets

$client = new Client(['base_uri' => 'http://my-app.com/api']); // no ending slash in base path - ignored
$response = $client->get('facets');
//    http://my-app.com/facets

$client = new Client(['base_uri' => 'http://my-app.com/api']);
$response = $client->get('/facets'); // leading slash - absolute path - base path is lost
//    http://my-app.com/facets

Tags:

Php

Guzzle