Avatar of James Murphy
James Murphy
Flag 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?


JSONPHP

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
Chris Stanyon

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
Michel Plungjan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
James Murphy

ASKER
Thank you gentlemen! it is now working exactly as I needed!

cheers

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Michel Plungjan

@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
Chris Stanyon

@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.