Link to home
Start Free TrialLog in
Avatar of James Murphy
James MurphyFlag for Australia

asked on

Could you please point out where I went wrong in applying variables to the json submission?

Hi,

I have 2 snippets that are supposed to be the same.


This code works:

$curl = curl_init();
curl_setopt_array($curl, array(   CURLOPT_URL => "https://api.creditsoft.com.au/v2/tica/",   CURLOPT_RETURNTRANSFER => true,   CURLOPT_ENCODING => "",   CURLOPT_MAXREDIRS => 10,   CURLOPT_TIMEOUT => 0,   CURLOPT_FOLLOWLOCATION => true,   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,   CURLOPT_CUSTOMREQUEST => "POST",   CURLOPT_POSTFIELDS =>"{\r\n    \"Requests\":[ \r\n    {\r\n        \"GivenName\": \"JOE\",\r\n        \"Surname\": \"BLOGGS\",\r\n        \"DOB\": \"08/04/1983\",\r\n        \"DriversLicenseNumber\":\"XXXXXXX\",\r\n        \"PassportNumber\": \"\",\r\n        \"ProofOfAgeCardNumber\": \"\",\r\n        \"ReferenceNumber\": \"CSOFT\"\r\n\r\n}\r\n]\r\n\r\n\r\n\r\n\r\n}",   CURLOPT_HTTPHEADER => array(     "Content-Type: application/json"   ), )); $response = curl_exec($curl); curl_close($curl); echo "<br><br>This is the response:<br><br>"; echo $response;

Open in new window

This code doesn't work:

$givenname = $data[0];
$lastname = $data[1]; $dob = $data[2];    $driverslicense = $data[3];    $passportnumber = $data[4];    $proofofage = $data[5];    $reference = $data[6];    echo "givename:".$givenname." surname:".$lastname." file ref:".$reference."<br><br>";    $curl = curl_init(); curl_setopt_array($curl, array(   CURLOPT_URL => "https://api.creditsoft.com.au/v2/tica/",   CURLOPT_RETURNTRANSFER => true,   CURLOPT_ENCODING => "",   CURLOPT_MAXREDIRS => 10,   CURLOPT_TIMEOUT => 0,   CURLOPT_FOLLOWLOCATION => true,   CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,   CURLOPT_CUSTOMREQUEST => "POST",   CURLOPT_POSTFIELDS =>"{\r\n    \"Requests\":[ \r\n    {\r\n        \"GivenName\": \"".$givenname."\",\r\n        \"Surname\": \"".$lastname."\",\r\n        \"DOB\": \"".$dob."\",\r\n        \"DriversLicenseNumber\":\"".$driverslicense."\",\r\n        \"PassportNumber\": \"".$passportnumber."\",\r\n        \"ProofOfAgeCardNumber\": \"".$proofofage."\",\r\n        \"ReferenceNumber\": \"".$reference."\"\r\n\r\n}\r\n]\r\n\r\n\r\n\r\n\r\n}",   CURLOPT_HTTPHEADER => array(     "Content-Type: application/json"   ), )); $response = curl_exec($curl); curl_close($curl); echo "<br><br>This is the response:<br><br>"; echo $response;

Open in new window

The second snippet is where I am trying to have variables rather than fixed values, could someone advise where I have gone wrong?


Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hey there,

You should add in some error handling to see if it gives you additional info:

if (! $response = curl_exec($curl)) {
    echo 'Curl error: ' . curl_error($curl);
} else {
    echo "<br><br>This is the response:<br><br>";
    echo $response;
}

curl_close($curl);

Open in new window

I would also suggest you send an array (json_encoded if needed) for your POST FIELDS instead of manually trying to build a string:

$payload = [
    'Requests' => [[
        'GivenName' => $givenname,
        'Surname' => $lastname,
        'DOB' => $dob,
    ]]
];

...

CURLOPT_POSTFIELDS => json_encode($payload),

Open in new window

SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of James Murphy

ASKER

thank you! I will take a look when I get back to my pc! the building the array option rather than the manual string should possibly do it. but in the manual string as such, I don't need to do the concatenation and can just have the variables there?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you gentlemen! it is now working exactly as I needed!

cheers

@James, great to hear

I would strongly suggest your consider @Chris' suggeestion of having the much more readable and editable object he suggests and json_encode it
@James,

If you do go the route of manually creating your JSON, then you're going to need some additional steps in place. You're not escaping any of your data, so if the user enters specific characters (quotes / backslashes etc), your code is going to blow up - you're not gonna get valid JSON

By manually creating your JSON like that, you've got far too many error vectors to use in production code. Go with json_encode() - it will escape everything for you.