我想將JSON發送到java web服務,但從參數如下的web服務獲得響應。我的代碼有什麼問題嗎?PHP cURL JSON發送空白參數
$buildApplication = array(
'firsname' => 'Keith',
'surname' => 'Francis',
'companyName' => 'Keiths Mobile Discos',
'phone' => '07123456789',
'email' => '[email protected]',
'sourceCode' => 'W00T'
);
$data = json_encode($buildApplication);
$ch = curl_init('http://10.50.1.71:8080/SME/api/details.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
var_dump($result);
響應返回
string(1042) "{"errors":[{"object":"com.application.AppDetails","field":"firstname","rejected-value":null,"message":"Property [firstname] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"surname","rejected-value":null,"message":"Property [surname] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"companyName","rejected-value":null,"message":"Property [companyName] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"phone","rejected-value":null,"message":"Property [phone] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"email","rejected-value":null,"message":"Property [email] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"sourceCode","rejected-value":null,"message":"Property [sourceCode] of class [class com.application.AppDetails] cannot be null"}]}"
你爲什麼JSON編碼兩次? – Harikrishnan
@Tom有一個錯字。你已經發布了'firsname'而不是'firstname' – AnkiiG