GuzzleHttp\Client change base url dynamically

Simeon Kolev picture Simeon Kolev · Feb 2, 2015 · Viewed 7.7k times · Source

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.

Answer

Konstantin Rachev picture Konstantin Rachev · Sep 22, 2015

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