James Murphy
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;
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;
The second snippet is where I am trying to have variables rather than fixed values, could someone advise where I have gone wrong?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you gentlemen! it is now working exactly as I needed!
cheers
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
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.
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.
You should add in some error handling to see if it gives you additional info:
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:Open in new window