如何使用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時,它不會提交。
什麼是JSON您要提交什麼樣子? – Sammitch
console.log($ jsonDataEncoded)並將其與您想要的輸出進行比較。 –
https://3v4l.org/ugIje確認這與您發佈的json完全相同。順便問一句,你得到的錯誤是什麼? – IROEGBU