-1

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...

21
  • 1
    Can you confirm when your print $_GET['data'] tht it is encoded. Commented Feb 16, 2015 at 11:28
  • 1
    Take a look at the source code of var_dump(base64_decode($_GET['data']));, maybe it's html encoded... Commented Feb 16, 2015 at 11:28
  • 3
    Might be a good idea to include the actual base64 string that you are trying to decode. Or at least the actual JSON data. have you tried to echo json_last_error(); to see what error the json parser encounters? Commented Feb 16, 2015 at 11:28
  • 1
    But did you try to do what i actually said in my comments? Also, don't answer by edits, thats just silly! Commented Feb 16, 2015 at 11:37
  • 1
    If json_last_error returns 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. Commented Feb 16, 2015 at 11:53

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.