HTTP请求中一个参数的多个值



我正在Laravel中使用一个外部API,在那里我必须发出以下请求。

每个产品都要重复一个参数产品,但当发送下面的请求时,端点只识别出产品的最后一个值。

$action = Http::get('https://http.cc/api/call.xml', [
'authid' => env('API_USER'),
'api-key' => env('API_KEY'),
'product' => 'Some Value',
'product' => 'Some Value',
'customer' => 1,
]);

我也尝试过以下但失败了:

'product[]' => 'Some Value',
'product[]' => 'Some Value',
'product' => ['Some Value', 'Some Value']

有人能帮我解决这个问题吗。

API:https://manage.logicboxes.com/kb/answer/776(可以参考ns参数(

感谢您的链接。如果查看ns参数,它有一个名为"Array of Strings"的链接。您可以单击它以获取解释。它给出了一个例子:

Example: ?variable=1&variable=2&variable=3&variable=4

现在我能理解你的问题了。Laravel不支持多个同名变量。但是,您仍然可以使用它,但是您必须构建自己的查询字符串。像这样:

// start by building a normal query string
$query = http_build_query([
'authid'   => env('API_USER'),
'api-key'  => env('API_KEY'),
'customer' => 1,
]);
// then add your products
foreach ($products as $product) {
$query .= '&product=' . urlencode($product);
}
// finally build the url yourself
$action = Http::get('https://http.cc/api/call.xml?' . $query);

请参阅:http_build_query((和urlencode((。

最新更新