Issue
When I send my json encoded array via
<?php
$data=array("Arzt"=>array("Kundennummer"=>"",
"Praxisname"=>"Praxis XY",
"Vorname"=>"Test",
"Nachname"=>"Test",
# ...
));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Authorization: Basic MTIzMTIzMS1CbGltdXM6clVja20yaVJxT0V2Y1ZTAb1IQUI='));
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
When CURL sends this, it arrives at the server encoded like this (webservice log):
{"{\"Arzt\":{\"Kundennummer\":\"\",\"Praxisname\":\"Praxis XY\",\"Vorname\":\"Test\",\"Nachname\": .........
But expected is:
{"Arzt":{"Kundennummer":"","Praxisname":"Praxis XY","Vorname":"Test","Nachname": ..........
How do I prevent the double encoding of the JSON string in PHP curl?
Solution
I had called
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(...));
several times and thought, that each header is added, but infact only the last header is added to the call.
You have to add all headers to one array and use curl_setopt for CURLOPT_HTTPHEADER only once.
In this case the jon header was overwritten, so the receiving end didn’t understand the data to be JSON but is interpreting it as regular form-encoded data, meaning it’s looking for a=b
values. So it’s giving you the entire JSON string as a key in an object with an empty value ({"{…}": ""})
Answered By - rubo77 Answer Checked By - Marie Seifert (WPSolving Admin)