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