5

All,

I make a JSON request to a web server using PHP and it returns me a JSON response in a variable. The JSON response will have lots of keys and values. The JSON response I get from the server has special characters in it. So, I use the following statement to convert it to UTF8,decode the JSON and use it as an array to display to the UI.

$response = json_decode(utf8_encode($jsonresponse));

Now, I have to pass the same value to the server in a JSON request to do some stuff. However, when I pass

$jsonrequest = json_encode(utf8_encode($request));

to the server, it fails.

The following code succeeds in reading special characters and displaying it to the UI. But fails if I have to pass the utf8_encode value to the server.

The current whole roundtrip code is as under:

$requestdata  = json_encode($request);
$jsonresponse = //Do something to get from server;
$response = json_decode(utf8_encode($jsonresponse));

How can I modify it such that I pass the exact value as to what I receieved from the json response from the server?

5
  • I had this issue recently, turned out I used to be a shit coder and used require_once in a function for some stupid reason years ago in a piece of code that I recycled. Good 3 hours of debugging. :) Commented Oct 8, 2017 at 12:25
  • @Jek What does require_once have to do with the encoding? Commented Nov 14, 2017 at 14:26
  • @Borna I had no clue why, but it resolved my issue. I spent hours narrowing down where in my codebase that the encoding issue was being printed out before JSON and I narrowed it down to calling require_once in a class' public function. Stupid thing. Commented Nov 14, 2017 at 20:06
  • @Jek hmm, it's so hard to believe... if it's 'require' and not 'require_once' maybe I would believe, just maybe :) Commented Nov 16, 2017 at 15:33
  • As I said before, no clue why it happened either, but that resolved my issue. I somewhat have a more understood belief learning about buffers and such. Commented Nov 19, 2017 at 6:35

5 Answers 5

8

The JSON response I get from the server has special characters in it. So, I use the following statement to convert it to UTF8,decode the JSON and use it as an array to display to the UI.

JSON data already comes encoded in UTF-8. You shouldn't convert it to UTF-8 again; you'll corrupt the data.

Instead of this:

$response = json_decode(utf8_encode($jsonresponse));

You should have this:

$response = json_decode($jsonresponse); //already UTF-8!
Sign up to request clarification or add additional context in comments.

2 Comments

+1, also, utf8_decode and utf8_encode should never be used because in practice you are never dealing with ISO-8859-1 encoding, and even if you need to convert between that and UTF-8 you would use iconv or mb_convert_encoding instead. People curious at how to do unicode in php properly should look at these slides: slideshare.net/auroraeosrose/using-unicode-with-php-30427880
(Windows Notepad issue) Please, consult this, I shared the problem too and it fixed it: stackoverflow.com/questions/10290849/…
3

Set This into your php connection :

$sql = “SET NAMES ‘utf8′”;
mysql_query($sql, $link);

Comments

2

have you tryed switching the places of the functions?

$jsonrequest = utf8_encode(json_encode($request));

utf8_encode only encodes strings not arrays

2 Comments

json_encode only works on utf-8 strings, so if there are non-utf-8 strings in $request, this will fail. (And if there aren't, there's no point in wrapping it in utf8_encode.)
Christian suggestion make sense, at least in my case, I was doing "$value = utf8_decode (json_encoded_value) and then $value = json_decode ($value). He's saying invert the order, do first $value = json_decode (json_encoded_value) and then $value = utf8_decode ($value). I believe because previous comments other developers will discard this answer, when it could be the solution in most cases.
1

You convert the initial request to UTF-8, so I assume it's something else. But when you send the data back, you do not convert it back to the original encoding.

Are you sure you send the data in the encoding expected by the server?

Comments

0

I also use both ZF and utf-8 encoded strings in AJAX calls and I think that the uft8_encode and utf8_decode functions should be obsolete.

Do you have a valid meta tag

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" / >

and a valid doctype

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

?

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.