Building query string programmatically in Guzzle?

旧城冷巷雨未停 提交于 2019-12-01 14:07:33

问题


In my PHP Guzzle client code, I have something like

$c = new Client('http://test.com/api/1.0/function');

$request = $c->get('?f=4&l=2&p=3&u=5');

but instead I want to have something like:

$request->set('f', 4);
$request->set('l', 2);
$request->set('p', 3);
$request->set('u', 5);

Is it possible in Guzzle? From the documentation and random googling it would seem it is, but I can't find exactly how.


回答1:


You can:

$c = new Client('http://test.com/api/1.0/function');

$request = $c->get();

$q = $request->getQuery();

$q->set('f', 4);
$q->set('l', 2);
$q->set('p', 3);
$q->set('u', 5);



回答2:


Guzzle 6 - you could use query option param

// Send a GET request to /get?foo=bar
$client->request('GET', '/get', ['query' => ['foo' => 'bar']]);

http://docs.guzzlephp.org/en/stable/request-options.html#query



来源:https://stackoverflow.com/questions/12283075/building-query-string-programmatically-in-guzzle

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