I have a JSON-String like
{"aaa":"foo", "bbb":"bar", "ccc":"hello", "ddd":"world"}
Actually I recive this string via $_GET. It is base64 encoded and if I decode it I have this string.
So further is want to use this in PHP as an object. For this is do
$data = json_decode( base64_decode( $_GET['data'] ) );
but $data is NULL all the time. If I do
echo base64_decode( $_GET['data'] );
Then the valid JSON-String is printed as expacted.
What am I doing wrong ?
I have tried to call the base64_encode before but same result...
1 Answer 1
Check json_last_error() and see what error the JSON parser encountered. If you encounter 5 then it is very likely that your JSON data contains unencoded UTF-8 sequences. You should always use a JSON-encoding library for handling data export to json.
See http://php.net/manual/en/function.json-last-error.php for a list of what errors you can handle with json_last_error (There is a INT to definition name table in the user comments)
0 = JSON_ERROR_NONE
1 = JSON_ERROR_DEPTH
2 = JSON_ERROR_STATE_MISMATCH
3 = JSON_ERROR_CTRL_CHAR
4 = JSON_ERROR_SYNTAX
5 = JSON_ERROR_UTF8
var_dump(base64_decode($_GET['data']));, maybe it's html encoded...echo json_last_error();to see what error the json parser encounters?json_last_errorreturns 5 then it is very likely that the JSON you have is in fact not valid, as it contains malformed UTF-8 sequences. I really can't help you without seeing the data you are handling.