2016-03-03 81 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 ='。哎呀!