1

How can I create this JSON output using PHP?

{
 "external_ref": "12345",
 "sale_datetime": "2016-03-01 22:09:00",
 "customer_name": "Foo Bar",
 "shipping_address_1": "123 Test Street",
 "shipping_address_2": "",
 "shipping_address_3": "City",
 "shipping_address_4": "County",
 "shipping_postcode": "AB12 3AB",
 "shipping_country": "England",
 "shipping_country_code": "GB",
 "shipping_method": "STANDARD",
 "phone": "01234567890",
 "items": [
 {
 "external_ref": "12345",
 "style": "mens",
 "size": "Medium",
 "color": "White",
 "print_location": "front",
 "print_x_offset": "0",
 "print_y_offset": "0",
 "quantity": 1,
 "external_url": "url.png",
 "external_thumbnail_url": "url.jpg"
 }
 ]
}

I've tried this myself (below) but it's failing to submit to the API I'm sending it to:

//Initiate cURL.
$ch = curl_init($url);
$item = array(
 array(
 "external_ref" => 12345,
 "style" => "mens",
 "size" => "Medium",
 "color" => "White",
 "print_location" => "FRONT",
 "print_x_offset" => "0",
 "print_y_offset" => "0",
 "quantity" => 1,
 "external_url" => "url.png",
 "external_thumbnail_url" => "url.jpg"
 )
);
//The JSON data.
$jsonData = array(
 "external_ref"=> 12345,
 "sale_datetime" => "2016-03-01 22:09:00",
 "customer_name" => "Foo Bar",
 "shipping_address_1" => "123 Test Street",
 "shipping_address_2" => "",
 "shipping_address_3" => "City",
 "shipping_address_4" => "County",
 "shipping_postcode" => "AB12 3AB",
 "shipping_country" => 'England',
 "shipping_country_code" => "GB",
 "shipping_method" => "STANDARD",
 "phone" => "01234567890",
 "items" => $item
);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);

Am I missing something important out here? The first bit of JSON code that at the top of this question does submit correctly. When I try to replicate that JSON with this PHP it doesn't submit.

asked Mar 3, 2016 at 17:56
6
  • What does the JSON you're submitting look like? Commented Mar 3, 2016 at 17:59
  • console.log($jsonDataEncoded) and compare it to your desired output. Commented Mar 3, 2016 at 17:59
  • 3v4l.org/ugIje confirm that this gives exactly the same json as the one you posted. What is the error you are getting, by the way? Commented Mar 3, 2016 at 18:02
  • @OliverQueen, no it is a nested array, hence the [ { instead of just {. Commented Mar 3, 2016 at 18:05
  • there is difference in sale_date_format Commented Mar 3, 2016 at 18:05

2 Answers 2

2

I run your code and get:

{
 "external_ref": 12345,
 "sale_datetime": "2016-03-01 22:09:00",
 "customer_name": "Foo Bar",
 "shipping_address_1": "123 Test Street",
 "shipping_address_2": "",
 "shipping_address_3": "City",
 "shipping_address_4": "County",
 "shipping_postcode": "AB12 3AB",
 "shipping_country": "England",
 "shipping_country_code": "GB",
 "shipping_method": "STANDARD",
 "phone": "01234567890",
 "items": [
 {
 "external_ref": 12345,
 "style": "mens",
 "size": "Medium",
 "color": "White",
 "print_location": "FRONT",
 "print_x_offset": "0",
 "print_y_offset": "0",
 "quantity": 1,
 "external_url": "url.png",
 "external_thumbnail_url": "url.jpg"
 }
 ]
}

difference in sale_datetime format:

  • 01 Mar 2016 22:09:00 - your desired format
  • 2016年03月01日 22:09:00 - php output in this fromat

and external_ref become integer not string

answered Mar 3, 2016 at 18:03

4 Comments

Even 'print_location' in 'items' subarray differs in CASE
Yes, you are right:) but author explicitly say "print_location" => "FRONT" in his code
I've made all cases such as "front" correct with their code now, but I still get the same error. Is The resource you are looking for has been removed, had its name changed, or is temporarily unavailable a generic PHP/cURL error? I've never seen it before and they haven't either.
Can you add output of curl_exec($ch) and curl_error($ch) to your question?
0

It turned out my url that the request was being submitted to was missing a '?' where I was adding the 'token='. Oops!

answered Mar 4, 2016 at 8:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.