At the risk of duplication, here is my very own struggle with json_decode(). I've been through many if not all the posts on this and nothing has worked. So here goes:
Here is a sample of my json variable's content:
{"id":"212","game":"36","player":"1","currentChallenge":"3","startDate":"1456575719","endDate":"1456575918","attempts":"0","completed":"1"}
I checked it on http://jsonlint.com/ and it's good
I am running these filters as per other forum suggestions:
$response = utf8_encode($response);
$response = str_replace('"', '"', $response);
And here is my decode line:
var_dump(json_decode($response, true));
I still get a NULL response and a syntax error (json_last_error()).
This data is coming from my own database and api. I tried inputting manually to the database and querying that, figuring I could get rid of any problems created by the input process. No luck there. And I have also sampled various tables to be sure it is not a problem related to the database setup.
And here is all my code:
if (isset($_POST['submit']) ) {
$table = $_POST['table'];
$id = $_POST['id'];
$url = 'http://example.com/api.php/' . $table . '/' . $id;
$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);
if ($response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
$response = utf8_encode($response);
$response = str_replace('"', '"', $response);
echo $response;
echo '<br />Result of json_decode: ';
var_dump(json_decode($response, true));
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
Thank you!
Copyright Notice:Content Author:「user2073143」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35781163/php-json-decode-returns-null-with-syntax-error