[英]PHP POST not working. Sending but NULL params?
I have an issue when trying to send JSON to a Java Web Service. 尝试将JSON发送到Java Web服务时遇到问题。 I am using cURL to post JSON however the web service responds that the paramters I send are NULL see error message below.
我使用cURL发布JSON但是Web服务响应我发送的参数为NULL,请参阅下面的错误消息。
$data = "{'firstname': 'tom', 'surname' : 'tom', 'companyName' : 'test','phone' : 01234567, 'email' : 'test@test.com'}";
$ch = curl_init('http://10.50.1.71:8080/SME/api/details.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($buildApplicationJSON))
);
$result = curl_exec($ch);
var_dump($result);
and the response I get is - 我得到的回应是 -
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 cla
string(1042)“{”errors“:[{”object“:”com.application.AppDetails“,”field“:”firstname“,”rejected-value“:null,”message“:”属性[firstname] of class [class com.application.AppDetails]不能为null“},{”object“:”com.application.AppDetails“,”field“:”surname“,”rejected-value“:null,”message“:”Property类[class com.application.AppDetails]的[surname]不能为null“},{”object“:”com.application.AppDetails“,”field“:”companyName“,”rejected-value“:null,”message “:”类[com.application.AppDetails]的属性[companyName]不能为null“},{”object“:”com.application.AppDetails“,”field“:”phone“,”rejected-value“: null,“message”:“类[com.application.AppDetails]类的属性[phone]不能为null”},{“object”:“com.application.AppDetails”,“field”:“email”,“reject” -value“:null,”message“:”类[class com.application.AppDetails]的属性[email]不能为null“},{”object“:”com.application.AppDetails“,”field“:”sourceCode “,”reject-value“:null,”message“:”cla的属性[sourceCode] ss [class com.application.AppDetails] cannot be null"}]}"
ss [class com.application.AppDetails]不能为null“}]}”
UPDATE: Still not working. 更新:仍然无法正常工作。 The $data JSON line was not an issue.
$ data JSON行不是问题。 In my previous version I had an array and used json_encode
在我之前的版本中,我有一个数组并使用了json_encode
$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($buildApplicationJSON))
);
$result = curl_exec($ch);
var_dump($result);
Your JSON is incorrect. 您的JSON不正确。 See the result of this jsfiddle .
看到这个jsfiddle的结果。
Now you could rewrite this string to be proper JSON, but that is quite error-prone. 现在您可以将此字符串重写为正确的JSON,但这很容易出错。 Instead, let
json_encode
do the work for you. 相反,让
json_encode
为您完成工作。
Define $data
as a PHP array: 将
$data
定义为PHP数组:
$data = ['firstname' => 'tom', 'surname' => 'tom', 'companyName' => 'test', 'phone' => 01234567, 'email' => 'test@test.com'];
Then use json_encode
when you want to pass it to curl 然后在想要将其传递给curl时使用
json_encode
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
Better yet, try to encode it before hand and check that it was successfuly encoded. 更好的是,尝试事先对其进行编码并检查它是否已成功编码。
if( $jsondata = json_encode($data) ){
//$jsondata is valid json
}
The string you send is not valid JSON. 您发送的字符串不是有效的JSON。 That is why the receiving side fails to extract values from it.
这就是接收方无法从中提取值的原因。
Note that there is a difference between: 请注意,以下内容之间存在差异:
{'firstname': 'tom', 'surname' : 'tom', 'companyName' : 'test','phone' : 01234567, 'email' : 'test@test.com'}
and 和
{"firstname": "tom", "surname": "tom", "companyName": "test", "phone": 342391, "email": "test@test.com"}
It usually is safest if you use phps json encoding function instead of trying to hard code things: 如果你使用phps json编码函数而不是尝试硬编码,通常是最安全的:
<?php
$data = json_encode([
'firstname' => 'tom',
'surname' => 'tom',
'companyName' => 'test',
'phone' => 01234567,
'email' => 'test@test.com'
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.