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?

Jake
  • 25,479
  • 31
  • 107
  • 168
  • 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. :) – Jack Hales Oct 08 '17 at 12:25
  • @Jek What does require_once have to do with the encoding? – Boy Nov 14 '17 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. – Jack Hales Nov 14 '17 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 :) – Boy Nov 16 '17 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. – Jack Hales Nov 19 '17 at 06:35

5 Answers5

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!
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • +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: http://www.slideshare.net/auroraeosrose/using-unicode-with-php-30427880 – Joeri Sebrechts Jan 29 '14 at 09:44
  • (Windows Notepad issue) Please, consult this, I shared the problem too and it fixed it: http://stackoverflow.com/questions/10290849/how-to-remove-multiple-utf-8-bom-sequences-before-doctype – Felix Aballi Sep 19 '14 at 14:55
3

Set This into your php connection :

$sql = “SET NAMES ‘utf8′”;
mysql_query($sql, $link);
gwdp
  • 1,202
  • 10
  • 21
2

have you tryed switching the places of the functions?

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

utf8_encode only encodes strings not arrays

Amir Hassan Azimi
  • 9,180
  • 5
  • 32
  • 43
Christian Smorra
  • 1,756
  • 11
  • 13
  • 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.) – Nathan Stretch Jul 01 '14 at 09:41
  • 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. – Gustavo Rodríguez Apr 22 '19 at 11:04
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?

Kwebble
  • 2,065
  • 14
  • 23
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">

?

aletzo
  • 2,471
  • 1
  • 27
  • 31