Tom
Tom

Reputation: 51

PHP POST not working. Sending but NULL params?

I have an issue when trying to send JSON to a Java Web Service. I am using cURL to post JSON however the web service responds that the paramters I send are NULL see error message below.

$data = "{'firstname': 'tom', 'surname' : 'tom', 'companyName' : 'test','phone' : 01234567, 'email' : '[email protected]'}";                                                                                                              
$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 class [class com.application.AppDetails] cannot be null"}]}"

UPDATE: Still not working. The $data JSON line was not an issue. In my previous version I had an array and used json_encode

$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($buildApplicationJSON))                                                                       
);                                                                                                                
$result = curl_exec($ch);
var_dump($result);

Upvotes: 3

Views: 1340

Answers (2)

Dan
Dan

Reputation: 11084

Your JSON is incorrect. See the result of this jsfiddle.

Now you could rewrite this string to be proper JSON, but that is quite error-prone. Instead, let json_encode do the work for you.

Define $data as a PHP array:

$data = ['firstname' => 'tom', 'surname' => 'tom', 'companyName' => 'test', 'phone' => 01234567, 'email' => '[email protected]'];

Then use json_encode when you want to pass it to curl

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
}

Upvotes: 3

arkascha
arkascha

Reputation: 42885

The string you send is not valid 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' : '[email protected]'}

and

{"firstname": "tom", "surname": "tom", "companyName": "test", "phone": 342391, "email": "[email protected]"}

It usually is safest if you use phps json encoding function instead of trying to hard code things:

<?php
$data = json_encode([
    'firstname' => 'tom',
    'surname' => 'tom',
    'companyName' => 'test',
    'phone' => 01234567,
    'email' => '[email protected]'
]);

Upvotes: 0

Related Questions