在 PHP 中创建此 JSON 输出


Create this JSON output in PHP?

如何使用

PHP创建此JSON输出?

{
  "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"
    }
  ]
}

自己尝试过这个(下面),但它无法提交到我将其发送到的 API:

//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);

我在这里错过了什么重要的东西吗?此问题顶部的第一段 JSON 代码确实正确提交。当我尝试使用此PHP复制该JSON时,它不会提交。

我运行你的代码并得到:

{
  "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"
    }
  ]
}

sale_datetime格式的差异:

  • 01 三月 2016 22:09:00 - 您想要的格式
  • 2016-03-01 22:09:00 - PHP输出在此来自

并且external_ref变成整数而不是字符串

事实证明,我正在向其提交请求的网址缺少一个"?",我在其中添加了"token="。哎呀!