2016-03-03 82 views
1

如何使用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": "County", 
    "shipping_postcode": "AB12 3AB", 
    "shipping_country": "England", 
    "shipping_country_code": "GB", 
    "shipping_method": "STANDARD", 
    "phone": "", 
    "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" => "", 
    "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时,它不会提交。

+0

什么是JSON您要提交什么样子? – Sammitch

+0

console.log($ jsonDataEncoded)并将其与您想要的输出进行比较。 –

+0

https://3v4l.org/ugIje确认这与您发布的json完全相同。顺便问一句,你得到的错误是什么? – IROEGBU

回答

2

我运行代码,并获得:在sale_datetime格式

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

区别:

  • 2016年3月1日22时09分00秒 - 你想要的格式
  • 2016年3月1日22 :09:00 - 这个fromat中的php输出

and external_ref变成整数不是字符串

+0

即使在“项目”子阵“print_location”大小写不同 –

+0

是的,你是对的:),但是笔者明确地说“print_location” =>“FRONT”在他的代码 –

+0

我做了所有的情况下,如“前”与正确他们的代码现在,但我仍然得到相同的错误。 “您正在查找的资源是否已被删除,名称已更改,或者暂时不可用”一个通用的PHP/cURL错误?我从来没有见过它,他们也没有。 –

0

原来我的URL请求被提交给缺少一个“?”在那里我添加了'token ='。哎呀!