Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GuzzleHttp\Client change base url dynamically

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.

like image 325
Simeon Kolev Avatar asked Feb 02 '15 12:02

Simeon Kolev


2 Answers

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

$client->setDefaultOption('base_uri', 'https://example.url');
like image 186
alioygur Avatar answered Oct 17 '22 07:10

alioygur


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

like image 45
Konstantin Rachev Avatar answered Oct 17 '22 06:10

Konstantin Rachev