我想通过guzzle将get-query发送到API。如果我使用这种方法
$test = [
'unum' => '0256',
'size' => 'size',
'dough' => 'dough'
];
$response = $this->guzzleClient->get('ajax/getprice.php?for_send=', [
'query' => $test
]);
效果最好。
但是,如果我想发送一个嵌套数组,就像这样:
$test = [
[
'unum' => '0256',
'size' => 'big',
'dough' => 'thin'
],
[
'unum' => '0256',
'size' => 'big',
'dough' => 'thin'
]
];
$response = $this->guzzleClient->get('ajax/getprice.php?for_send=', [
'query' => $test
]);
不起作用。
但我确定API会接受此类请求(看起来像ajax/getid.php?for_send=%5B%7B%22unum%22%3A%220141%22%2C%22dough%22%3A%22%D0%9F%D1%8B%D1%88%D0%BD%D0%BE%D0%B5%20%D1%82%D0%B5%D1%81%D1%82%D0%BE%22%2C%22size%22%3A%22%D0%91%D0%BE%D0%BB%D1%8C%D1%88%D0%B0%D1%8F%2C%2035%20%D1%81%D0%BC%22%7D%2C%7B%22unum%22%3A%220256%22%2C%22dough%22%3A%22%D0%9F%D1%8B%D1%88%D0%BD%D0%BE%D0%B5%20%D1%82%D0%B5%D1%81%D1%82%D0%BE%22%2C...
)
如何从嵌套数组中生成有效的query
?
答案 0 :(得分:1)
根据您的示例,API在for_send
参数内接受JSON,因此只需将数组编码为JSON:
$test = [
[
'unum' => '0256',
'size' => 'Большая, 35 см',
'dough' => 'Тонкое тесто'
],
// ...
];
$response = $this->guzzleClient->get('ajax/getprice.php', [
'query' => ['for_send' => json_encode($test)]
]);