0

I am having trouble constructing a nested array properly to encode with JSON to post to an API. I think my code array structure looks good, but I must have an error as only the auth, order_number, shipping variables post. The order_items, variation_list, print_locations nested arrays do not post. I'm thinking I must be missing an extra set of array () for the nested array definitions. I tried a few variations, but none of my trial/error has worked. Unfortunately, I do not receive an error back from the API with my following code.

How am I formatting this array incorrectly? Any help is much appreciated.

My code:

$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";

Example JSON structure I was given:

{
    "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"
}] }] 
} 
3
  • This JSON can't be from the provided PHP. "location_name": "Full Back" isn't in the PHP sample. Could you check your code please?
    – tshimkus
    Dec 2, 2018 at 19:37
  • This wasn't required, so I left it out of the PHP. Dec 2, 2018 at 20:04
  • I misunderstood that. Thanks for clarifying.
    – tshimkus
    Dec 2, 2018 at 20:35

1 Answer 1

2

The elements that show brackets should be arrays of arrays, which makes sense given that "order_items" and "print_locations" can have multiple entries. You will need to nest the arrays a little deeper at those levels, like this:

'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"
            )
        )
    )
)

When you get to the actual implementation you will likely need to use some kind of loop on these fields to insert all the "order_items" and "print_locations"

1
  • Hi. This is the correct answer. Thank you so much for the help. I have not worked with nested arrays so much. Now I need to research the best way to build the arrays with a loop when dealing with multiple products. 1 step at a time. Thank you so much. Dec 2, 2018 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.