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

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • 1
    Can you confirm when your print $_GET['data'] tht it is encoded. – Antony D'Andrea Feb 16 '15 at 11:28
  • 1
    Take a look at the source code of `var_dump(base64_decode($_GET['data']));`, maybe it's html encoded... – HamZa Feb 16 '15 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? – EJTH Feb 16 '15 at 11:28
  • It is simply not a valid JSON-string, since a non valid json-string returns NULL. Please provide the actual output of the base64_decode. There is something wrong there. – W van Rij Feb 16 '15 at 11:29
  • what is the output of $data = json_decode( base64_decode( $_GET['data'] ), true ); – fortune Feb 16 '15 at 11:31
  • @fortune he said, its Null – Antony D'Andrea Feb 16 '15 at 11:32
  • Look at the actual source output of `echo base64_decode($_GET['data']);` rather than the browser-rendered result; as @HamZa says, perhaps you're getting `&quote;` rather than `"` in the data? See also http://stackoverflow.com/questions/6324645/problem-with-json-decode-php – cmbuckley Feb 16 '15 at 11:33
  • added a edit to my post to answere all comments – Dwza Feb 16 '15 at 11:34
  • This is not a forum - you do not answer comments by editing question! – Marcin Orlowski Feb 16 '15 at 11:35
  • @MarcinOrlowski so you want me to write 5 comments ? – Dwza Feb 16 '15 at 11:36
  • 1
    But did you try to do what i actually said in my comments? Also, don't answer by edits, thats just silly! – EJTH Feb 16 '15 at 11:37
  • @Dwza What do you mean "it's the same"? That even `json_decode( '{"aaa":"foo",....}');` doesn't work? If that's the case then you have a major problem. I hope you're not coding in MS-word :P – HamZa Feb 16 '15 at 11:37
  • {"aaa":"foo",....} isnt even valid json so there you go, problem solved! – EJTH Feb 16 '15 at 11:37
  • Sorry for editing my post and answere 5 questions to the same time... so ill go for comments end undid the changes in my post... – Dwza Feb 16 '15 at 11:40
  • also, check this out -> http://stackoverflow.com/questions/21529843/base64-decode-in-php-with-a-json-decode might be related. – rasso Feb 16 '15 at 11:41
  • @AntonyD'Andrea Yes i can. @HamZa No, its not. @ EJTH I also have tried to use `json_decode( '{"aaa":"foo",....}');` Its same. @HamZa I tested the string on Lint allready. @W van Rij jslint sayes its valid. – Dwza Feb 16 '15 at 11:42
  • @EJTH yes i did and it returns 5. Could this be a UTF8 problem ? – Dwza Feb 16 '15 at 11:45
  • did you try using `urldecode` before you call other functions? like `$data = json_decode(base64_decode(urldecode($_GET['data'])));` – rasso Feb 16 '15 at 11:49
  • @OzgurBar actually I don't need it since the json string is return as expacted but even with this I have the same result – Dwza Feb 16 '15 at 11:51
  • 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. – EJTH Feb 16 '15 at 11:53
  • @EJTH You are right. Just found out the same... actually it's some german words like `straße` and `fußgänger` so I oviously have to transform the data into some like unicode or some... – Dwza Feb 16 '15 at 11:57

1 Answers1

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
EJTH
  • 2,178
  • 1
  • 20
  • 25