Liam McArthur
Liam McArthur

Reputation: 1033

Create this JSON output in PHP?

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.

Upvotes: 1

Views: 46

Answers (2)

Liam McArthur
Liam McArthur

Reputation: 1033

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

Upvotes: 0

alexander.polomodov
alexander.polomodov

Reputation: 5534

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

Upvotes: 2

Related Questions