Tom February 2016

PHP cURL JSON sending blank paramaters

I am trying to send JSON to a java web service but getting response from web service that paramaters all null see below. Is there anything wrong with my code?

$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);

Response comes back

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         

Answers


AnkiiG February 2016

  • You have posted firsname instead of firstname.
  • Also you used json_encode twice.

Try as below :

$buildApplication = array(
    'firstname'          => '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, $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);


Ravi Hirani February 2016

You have used json_encode twice.

Write below line as:-

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  

Post Status

Asked in February 2016
Viewed 1,825 times
Voted 7
Answered 2 times

Search




Leave an answer