The problem is as follows. There is a contractor who sends a request to our json script to our server. An example query is as follows:

{"internal_id":"2", "name" :"Партнер", "logo" :"тестовые данные"} 

Sends it to the contractor through some kind of application program on windows 7, Assures that the json request in utf-8. During the transfer, manually register all headers with utf-8

I accept the request using php

Here is the script:

 include $_SERVER['DOCUMENT_ROOT']."/api/config.php"; header('Content-type: text/html; charset=utf-8'); $data = file_get_contents('php://input'); $result = mysql_query("INSERT INTO test3 (id, rek) VALUES ('', '$data')"); 

As a result, in the database I get

 {"internal_id":"2", "name" :"???????", "logo" :"????????"} 

That is, Cyrillic in the database is not recorded. In my DB, the table is of the type MyISAM, the rek field is encoding utf8_general_ci

Here is what I tried:

  1. I asked to generate a json file from this program, and then I tried to open it in my sublime. When I open my editor does not recognize the Cyrillic, Vidzhu crack. I manually tried to reopen the file through the sublime and it opens correctly in windows-1251.

As a result of this test, I understand that its request comes in windows-1251 to our server, and ignores all its headers apparently and is not encoded on its side in utf8.

I try to transcode manually using php and save:

 $data = file_get_contents('php://input'); $test1 = mb_convert_encoding($data, "utf-8", "cp1251"); $text2 = iconv('cp1251', 'UTF-8', $data); $result = mysql_query("INSERT INTO test3 (id, rek) VALUES ('', 'test1')"); 

As a result, even with forced recoding on my side, the Cyrillic in my database is not displayed on my side, but is shown either ???? or other cracks. I tried the above two functions, there is no positive result.

Then I tried to create a test3 table in the database, but already with cp1251_general_ci

The result is positive. Everything in the database is saved correctly.

The question is how can I still recode json, what else can I try, since I need to save the database in utf8 so that I do not have any problems later.

    2 answers 2

    Try using mb_detect_encoding to find out in which encoding the data arrives .

     $convert_data = mb_convert_encoding($json, 'utf8', mb_detect_encoding($json)); 

    And as I understand you have a connection to the database through the outdated mysql driver. Is the connection coding set when connecting to the DB?

     mysql_query("SET NAMES utf8", $link); 
    • Thank you, check and report the result - WhoIsDT
    • one
      Thank you your option solved the problem! mb detect worked) - WhoIsDT

    I understand that you code json on the client side. And try to decode and re-encode. PHP encodes UTF-8 characters and you get something like {"internal_id":"2","name":"\u041f\u0430\u0440\u0442\u043d\u0435\u0440","logo":"\u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435"}

     include $_SERVER['DOCUMENT_ROOT']."/api/config.php"; header('Content-type: text/html; charset=utf-8'); $data = file_get_contents('php://input'); $data = json_encode(json_decode($json)); $result = mysql_query("INSERT INTO test3 (id, rek) VALUES ('', '$data')");