适用于API卷曲帖子的JSON格式的正确嵌套数组

时间:2018-12-02 19:12:18

标签: php json curl

我在正确构造嵌套数组以使用JSON编码以发布到API时遇到麻烦。我认为我的代码数组结构看起来不错,但是由于auth,order_number,shipping变量发布,我必须有一个错误。不发布order_items,variation_list,print_locations嵌套数组。我在想必须为嵌套数组定义缺少额外的array()集。我尝试了一些变体,但没有一个试验/错误奏效。不幸的是,我的以下代码没有收到来自API的错误消息。

我如何正确格式化此数组?非常感谢您的帮助。

我的代码:

$data = array(
    'auth'=>"$auth_key",
    'order_number'=>"$order_number",
    'shipping'=> array (
            'first_name'=>"John",
            'last_name'=>"Doe",
            'address1'=>"123 Someplace St.",
            'address2'=>"Apt or Suite",
            'city'=>"Town",
            'state'=>"PA",
            'postal_code'=>"16125",
        'country'=>"US"
    ),
    'order_items'=> array (
            'name'=>"Dale Blaney 2016 Design",
            'quantity'=>"1",
            'code'=>"1081389-GSS-FB-2XL-BK",
            'variation_list'=> array (
                    'size'=>"2XL",
                    'color'=>"Black", 
                    'style'=>"5000"
        ),
            'print_locations'=> array (
                    'location_name'=>"Full Front",
                    'location_art'=>"http://somedomain.com/link_to_your_artwork.png",
                    'location_preview'=>"http://somedomain.com/link_to_your_preview.png"
        )
    )


);

$data = json_encode($data);

$url = "https://blah.com/api/process14504.asp";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$TS_Result = curl_exec($ch);
curl_close($ch);

echo "$TS_Result";

给出了示例JSON结构:

{
    "auth": "blahblahblah", 
    
     "order_number": 136794,
    "shipping": {
        "first_name": "John",
        "last_name": "Doe",
        "address1": "123 Someplace St.",
        "address2": "Apt or Suite",
        "city": "Town",
        "state": "PA", 
        "postal_code": "16125",
        "country": "US"
},
"order_items": [{
    "name": "Dale Blaney 2016 Design",
    "quantity": 1, 
    "code": "1081389-GSS-FB-2XL-BK",
    "variation_list": {
            "size": "2XL",
            "color": "Black",
            "style": "5000"
        },
        "print_locations": [{
            "location_name": "Full Front",
            "location_art": "http://somedomain.com/link_to_your_artwork.png", 
            }, 
       {"location_preview": "http://somedomain.com/link_to_your_preview.png"
            "location_name": "Full Back",
            "location_art": " http://somedomain.com/link_to_your_artwork.png",
            "location_preview": "http://somedomain.com/link_to_your_preview.png"
}] }] 
} 

1 个答案:

答案 0 :(得分:2)

显示方括号的元素应该是数组的数组,考虑到“ order_items”和“ print_locations”可以有多个条目,这是有意义的。您将需要在这些级别将数组嵌套得更深,例如:

'order_items'=> array (
    array( // one order item
        'name'=>"Dale Blaney 2016 Design",
        'quantity'=>"1",
        'code'=>"1081389-GSS-FB-2XL-BK",
        'variation_list'=> array (
            'size'=>"2XL",
            'color'=>"Black", 
            'style'=>"5000"
        ),
        'print_locations'=> array (
            array( // one print location
                'location_name'=>"Full Front",
                'location_art'=>"http://somedomain.com/link_to_your_artwork.png",
                'location_preview'=>"http://somedomain.com/link_to_your_preview.png"
            )
        )
    )
)

当您进入实际的实现时,您可能需要在这些字段上使用某种循环来插入所有的“ order_items”和“ print_locations”