简体   繁体   中英

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.

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

到Guzzle的第5版,您可以使用setDefaultOption方法:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM