I have a json response coming back from a webservice, which, to the naked eye is:
{ "fault": { "code" : "Application Error", "message" : "Generic Error Message" } }
However it fails to json_decode
- json_decode returns null.
Debugging json_decode with:
json_decode($response);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
returns the error: Syntax error, malformed JSON
Using my browser console, I can see the actual json string is:
 { "fault": { "code" : "Application Error", "message" : "Generic Error Message" } }
Note the extra at the start.
I have tried everything to remove this unseen character, but I cannot get rid of it!
e.g.
function cleanString($val)
{
$non_displayables = array(
'/%0[0-8bcef]/', # url encoded 00-08, 11, 12, 14, 15
'/%1[0-9a-f]/', # url encoded 16-31
'/[\x00-\x08]/', # 00-08
'/\x0b/', # 11
'/\x0c/', # 12
'/[\x0e-\x1f]/', # 14-31
'/x7F/' , # 127
'//' # 127
);
foreach ($non_displayables as $regex)
{
$val = preg_replace($regex,'',$val);
}
$search = array("\0","\r","\x1a","\t");
return trim(str_replace($search,'',$val));
}
plus tried using things like stripslashes
, html_entity_decode
etc
I tested a simple:
$string = str_replace("FEFF","",$string);
and it doesn't remove the FEFF element at all.
My headers are UTF-8.
If I try exactly the same request in Postman (the api tester tool), it works - Postman manages to decode the JSON and display it nicely.
So, somewhere along the line either Postman successfully removes this character, or I am adding it.
Below is the full CURL (PHP) request I am calling, whcih is exactly the same as the PHP Curl code outputted from the Postman call which works:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://example.com/?reference=Test',
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 =>'example',
CURLOPT_HTTPHEADER => array(
'Accept: Application/json',
'Subscription-Key: redacted',
'SOAPAction: example',
'Content-Type: text/plain',
'Cookie: Cookiesession=redacted'
),
));
$response = curl_exec($curl);
I have also tried the same call using GuzzleHTTP and I get the same issue.
How can I get rid of this character, and get the json to validate?
Thanks