我正在尝试将woocommerce的销售订单发送到外部API。我已经设法POST了json请求,并且可以看到它已到达,但我无法使结构正确,因此API接受请求。
json请求的结构如下所示
{
"customer": "",
"delivery_instructions": "",
"delivery_address": {
"address1": "",
"address2": ""
},
"payment": {
"method": "",
"transaction_id": "",
"amount": ""
},
"info": [
{
"type": "",
"code": "",
"quantity": ""
}
]
}
我无法正确格式化的是将请求发送到API的PHP,因为我错过了info数组周围的方括号
这是我目前的PHP:
// The data to send to the API
$postData = [
'customer' => '',
'delivery_instructions' => '',
'delivery_address' => [
'address1' => '',
'address2' => ''
],
'payment' => [
'method' => 'PP',
'transaction_id' => '',
'amount' => ''
],
'info' => [
'type' => '',
'item_code' => '',
'quantity' => ''
],
];
但是当我发布这个结果时,结果是
{
"customer": "",
"delivery_instructions": "",
"delivery_address": {
"address1": "",
"address2": ""
},
"payment": {
"method": "",
"transaction_id": "",
"amount": ""
},
"info": {
"type": "",
"code": "",
"quantity": ""
}
}
非常感谢任何指导。
答案 0 :(得分:0)
我建议你这个结构:
$postData = new stdClass;
$postData->customer = '';
$postData->delivery_instructions = '';
$postData->delivery_address = (object)array('address1' => '', 'address2' => '');
$postData->payment = (object)array('method' => '', 'transaction_id' => '', 'amount' => '');
$postData->info = array( (object)array('type' => '', 'code' => '', 'quantity' => '') );
print_r( json_encode($postData) );
答案 1 :(得分:0)
<?php
$postData = [
'customer' => '',
'delivery_instructions' => '',
'delivery_address' => [
'address1' => '',
'address2' => ''
],
'payment' => [
'method' => 'PP',
'transaction_id' => '',
'amount' => ''
],
'info' => [
'type' => '',
'item_code' => '',
'quantity' => ''
],
];
$val = array();
$val["type"] = '';
$val["item_code"] = '';
$val["quantity"] = '';
$data = array();
$data['info'][] = $val;
//echo json_encode($data);
$myvalues = array_merge($postData,$data);
echo '<pre>';
echo json_encode($myvalues);
echo '</pre>';
?>
{
"customer":"",
"delivery_instructions":"",
"delivery_address":{
"address1":"",
"address2":""
},
"payment":{
"method":"PP",
"transaction_id":"",
"amount":""
},
"info":[
{
"type":"",
"item_code":"",
"quantity":""
}
]
}
注意:这里我只为信息数组数据创建一个数组,然后输出合并到我们的实际
postData
数组中,然后回显..
答案 2 :(得分:-1)
在Php中,json数据近似于数组数据