PHP Guzzle-发送多个具有相同名称的查询参数

问题描述 投票:0回答:1

我有一个id数组,我想发送这样的GET请求:

https://api.com/endpoint?id=1&id=2&id=3&...

我以以下方式使用Guzzle:

$idArray = [1, 2, 3, 4, 5];
$guzzleClient->request("GET", "https://api.com/endpoint", ["query" => [
    'id' => $idArray
]])

但结果是这样:

https://api.com/endpoint?id%5B0%5D=15&id%5B1%5D=2...

并且我正在使用的API对此不起作用。

任何想法?

php guzzle
1个回答
0
投票

如果需要将数组转换为字符串,这会有所帮助。

$idString = '';

// Loop through the array to build up a string in the form of id[]=1&id[]=2 ...
foreach ($idArray as $id) { 
   $idString .= 'id[]=' . $value . '&';
}

// Remove the last & from the string
rtrim($idString , '&');

$guzzleClient->request("GET", "https://api.com/endpoint", ["query" => $idString])
© www.soinside.com 2019 - 2024. All rights reserved.