json - PHP POST not working. Sending but NULL params? -


i have issue when trying send json java web service. using curl post json web service responds paramters send null see error message below.

$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 response -

string(1042) "{"errors":[{"object":"com.application.appdetails","field":"firstname","rejected-value":null,"message":"property [firstname] of class [class com.application.appdetails] cannot null"},{"object":"com.application.appdetails","field":"surname","rejected-value":null,"message":"property [surname] of class [class com.application.appdetails] cannot null"},{"object":"com.application.appdetails","field":"companyname","rejected-value":null,"message":"property [companyname] of class [class com.application.appdetails] cannot null"},{"object":"com.application.appdetails","field":"phone","rejected-value":null,"message":"property [phone] of class [class com.application.appdetails] cannot null"},{"object":"com.application.appdetails","field":"email","rejected-value":null,"message":"property [email] of class [class com.application.appdetails] cannot null"},{"object":"com.application.appdetails","field":"sourcecode","rejected-value":null,"message":"property [sourcecode] of class [class com.application.appdetails] cannot null"}]}"

update: still not working. $data json line not issue. in previous version had array , used 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 incorrect. see result of jsfiddle.

now rewrite string proper json, quite error-prone. instead, let json_encode work you.

define $data php array:

$data = ['firstname' => 'tom', 'surname' => 'tom', 'companyname' => 'test', 'phone' => 01234567, 'email' => 'test@test.com']; 

then use json_encode when want pass curl

curl_setopt($ch, curlopt_postfields, json_encode($data)); 

better yet, try encode before hand , check successfuly encoded.

if( $jsondata = json_encode($data) ){     //$jsondata valid json } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -