【发布时间】:2016-02-08 09:02:57
【问题描述】:
我正在尝试将 JSON 发送到 java web 服务,但从 web 服务获得响应,该参数全部为空,见下文。我的代码有什么问题吗?
$buildApplication = array(
'firsname' => 'Keith',
'surname' => 'Francis',
'companyName' => 'Keiths Mobile Discos',
'phone' => '07123456789',
'email' => 'keith.francis@freedom-finance.co.uk',
'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为什么要编码两次?
-
@Tom 有一个错字。您已发布
firsname而不是firstname