base_uri not being based from guzzle client instantiation

巧了我就是萌 提交于 2020-01-31 04:32:25

问题


I'm using lumen trying to set up simple api requests via guzzle.

The problem is the base_uri parameter doesn't appear to be passed correctly on the initial new Client().

Simplified example:

use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => 'https://siteurl.com/api/v2'
]);

Then calling the api via get

$res = $client->get('orders', [
    'query' => [
        'status' => 'completed'
    ]
]);

does not work. I've been careful not to use absolute urls like /orders. If I bypass base_uri entirely and just add it on the get method $client->get('https://siteurl.com/api/v2/orders'), it works.

I'm using: "laravel/lumen-framework": "5.0.*", "guzzlehttp/guzzle": "^6.0"

*Follow-up:

I added the debug flag so I could compare the headers, and the noticable difference is in the get request line.

Absolute url in the get method (bypassing base_uri):

GET /api/v2/orders?status=completed HTTP/1.1

Using base_uri (version is being stripped):

GET /api/orders?status=completed HTTP/1.1


回答1:


You need to terminate your base_uri with a forward slash /

E.g.,

use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => 'https://siteurl.com/api/v2/'
]);

Edit: Note that base_uri is for Guzzle 6+, whereas previous versions used base_url.



来源:https://stackoverflow.com/questions/30834147/base-uri-not-being-based-from-guzzle-client-instantiation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!