在PHP中创建这个JSON输出?

在PHP中创建这个JSON输出?,php,json,Php,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": "Coun

如何使用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_日期时间格式的差异:

  • 2016年3月1日22:09:00-您想要的格式
  • 2016-03-01 22:09:00-此fromat中的php输出

而外部的_ref变为整数而不是字符串

结果是,请求提交到的url缺少一个“?”我在其中添加了“token=”。哎呀

您提交的JSON是什么样子的?console.log($jsonDataEncoded)并将其与所需的输出进行比较。确认这与您发布的json完全相同。顺便问一句,您得到的错误是什么?@OliverQueen,不,它是一个嵌套数组,因此是
[{
而不是
{
。销售日期格式不同,即使“items”子数组中的“print\u location”在大小写上有所不同是的,您是对的:)但作者明确地说“print\u location”=>“FRONT”在他的代码中,我已经用他们的代码纠正了所有情况,例如“front”,但我仍然得到相同的错误。您正在查找的资源是否已被删除、名称已更改或暂时不可用?是一个通用的PHP/cURL错误?我以前从未见过,它们也没有。您可以添加cURL\u exec($ch)的输出吗你的问题是什么?