是否可以在GuzzlePHP客户端传递GET
请求的数据数组?
我在文档和Stack Overflow上都没有找到任何内容。
即这不符合我的预期:
// first level of this array is refered as
// request settings: headers, redirects, etc.
$array = [
'this will be rendered as request settings',
'data' => [
'var1' => 'value1',
'var4' => 'value4',
],
];
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', '/redirect/3', $array);
修改:对于所有无法理解问题的人:
此请求中未传递GET参数,因此我只获得此URL结构:
http://host/redirect/3
预期:
http://host/redirect/3?var1=value1&var4=value4
答案 0 :(得分:2)
如果您打开手册,例如http://docs.guzzlephp.org/en/stable/quickstart.html#query-string-parameters,您会看到应在query
键下发送查询参数。所以你的数组应该是:
$array = [
'this will be rendered as request settings',
'query' => [
'var1' => 'value1',
'var4' => 'value4',
],
];
答案 1 :(得分:1)
终于找到了。设置密钥为query
如果anyone else需要它:
$params = [
'var1' => 'value1',
'var4' => 'value4',
];
$client->request('GET', '/redirect/3', [
'query' => $params
]);
这最终将我的请求的URL转换为:
/redirect/3?var1=value1&var4=value4