12
votes

I am working on a project with a restful API. I use GuzzleHttp library in order to get/put etc. data in the API.

Is there any way where you can change GuzzleHttp\Client's base url on the run. My idea is because I have multiple endpoints of one api but the base url is not the same so I want to create one Client with some options but change the base url when I want to execute some request.

2

2 Answers

11
votes

As it is written in GuzzleHttp's documentation, if you pass absolute path to post/get/put, etc. it will override the current base url. So, if you prepend the absolute path it will get the job done. Check here, GuzzleHttp documentation

5
votes

Up to version 5 of Guzzle, you can use setDefaultOption method:

$client->setDefaultOption('base_uri', 'https://example.url');