如何使用多个 ID PHP 遍历 CURLOPT_POSTFIELDS 数据
How to loop through CURLOPT_POSTFIELDS data with multiple id's PHP
我有一个包含多个订单项(特别是 15 个)的订单,我正在尝试使用参数遍历 id 我在这里遗漏了什么?:
foreach($line_ids as $key => $id ) {
$params = [
"order_lines" => [
"accepted" => true,
"id" => $id
]
];
$data = json_encode($params);
echo '<pre>';
print_r($data);
echo '</pre>';
}
输出看起来像这样:
{
"order_lines":{
"accepted":true,
"id":"75167652-1"
}
}
但每个订单项都需要这样:
{
"order_lines": [
{
"accepted": true,
"id": "75261431-1"
}
]
}
这是单个 ID 的 CURL:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $baseUrl . $order_id . '/accept',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS =>'{
"order_lines": [
{
"accepted": true,
"id": "75261431-1"
}
]
}
',
CURLOPT_HTTPHEADER => array(
'Authorization: xxxxxx-xxxxx-xxxxx-xxxxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
这是我用上面的 foreach 尝试过的多个 id 的 CURL。:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $baseUrl . $order_id . '/accept',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => array(
'Authorization: xxxxxx-xxxxx-xxxxx-xxxxx',
'Content-Type: application/json'
),
));
$accept_orders = curl_exec($curl);
curl_close($curl);
echo $accept_orders . '<br>';
但这是我为每个订单项获得的输出:
{ "message" : "Body is required", "status" : 400 }
你得到的错误与数组无关,它说数据发送为空。
这可能是您没有在 curl 连接上启用 POST 请求导致的。
CURLOPT_POST => true
数组示例;
<?php
$ids = array(1,2,3,4,5);
$params = array();
foreach($ids as $id)
{
$params['order_lines'][] = ['id' => $id,'accepted' => 'true'];
}
$json = json_encode($params,JSON_PRETTY_PRINT);
print_r($json);
?>
{
"order_lines": [
{
"id": 1,
"accepted": "true"
},
{
"id": 2,
"accepted": "true"
},
{
"id": 3,
"accepted": "true"
},
{
"id": 4,
"accepted": "true"
},
{
"id": 5,
"accepted": "true"
}
]
}
我有一个包含多个订单项(特别是 15 个)的订单,我正在尝试使用参数遍历 id 我在这里遗漏了什么?:
foreach($line_ids as $key => $id ) {
$params = [
"order_lines" => [
"accepted" => true,
"id" => $id
]
];
$data = json_encode($params);
echo '<pre>';
print_r($data);
echo '</pre>';
}
输出看起来像这样:
{
"order_lines":{
"accepted":true,
"id":"75167652-1"
}
}
但每个订单项都需要这样:
{
"order_lines": [
{
"accepted": true,
"id": "75261431-1"
}
]
}
这是单个 ID 的 CURL:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $baseUrl . $order_id . '/accept',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS =>'{
"order_lines": [
{
"accepted": true,
"id": "75261431-1"
}
]
}
',
CURLOPT_HTTPHEADER => array(
'Authorization: xxxxxx-xxxxx-xxxxx-xxxxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
这是我用上面的 foreach 尝试过的多个 id 的 CURL。:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $baseUrl . $order_id . '/accept',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => array(
'Authorization: xxxxxx-xxxxx-xxxxx-xxxxx',
'Content-Type: application/json'
),
));
$accept_orders = curl_exec($curl);
curl_close($curl);
echo $accept_orders . '<br>';
但这是我为每个订单项获得的输出:
{ "message" : "Body is required", "status" : 400 }
你得到的错误与数组无关,它说数据发送为空。
这可能是您没有在 curl 连接上启用 POST 请求导致的。
CURLOPT_POST => true
数组示例;
<?php
$ids = array(1,2,3,4,5);
$params = array();
foreach($ids as $id)
{
$params['order_lines'][] = ['id' => $id,'accepted' => 'true'];
}
$json = json_encode($params,JSON_PRETTY_PRINT);
print_r($json);
?>
{
"order_lines": [
{
"id": 1,
"accepted": "true"
},
{
"id": 2,
"accepted": "true"
},
{
"id": 3,
"accepted": "true"
},
{
"id": 4,
"accepted": "true"
},
{
"id": 5,
"accepted": "true"
}
]
}