简体   繁体   English

LURACAST Restler框架-处理欧洲字符时出现的问题

[英]LURACAST Restler framework - Issue in handling european characters

I am using restler PHP API to host a REST service. 我正在使用restler PHP API托管REST服务。 I am having a problem with handling some European characters, otherwise it is working fine. 我在处理某些欧洲字符时遇到问题,否则工作正常。

For example I have the string "Český rozhlas 1 - Radiožurnál" in a MySQL database. 例如,我在MySQL数据库中有字符串“Českýrozhlas 1-Radiožurnál”。 When the restler API converts the data in to JSON, it is converted like this "?esk\ý rozhlas 1 - Radio\žurn\ál" 当restler API将数据转换为JSON时,数据的转换方式如下:“?esk \\ u00fd rozhlas 1-Radio \\ u009eurn \\ u00e1l”

Here first character is converted as a question mark. 在这里,第一个字符被转换为问号。

How can I convert the data properly using the restler PHP service? 如何使用restler PHP服务正确转换数据?

When dealing with Unicode, we need to make sure we use utf-8 all the way 在处理Unicode时,我们需要确保一路使用utf-8

First you need to make sure MySQL database is using utf-8 encoding. 首先,您需要确保MySQL数据库正在使用utf-8编码。 You can run the following sql to make sure of that 您可以运行以下sql来确保

ALTER TABLE your_table_name_here CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

Next you need to make sure MySQL spits utf-8 when talking to PHP. 接下来,您需要确保MySQL与PHP对话时会吐出utf-8。

You can use the following commands 您可以使用以下命令

mysql_query('SET NAMES utf8');
mysql_query('SET CHARACTER SET utf8');

If you are using PDO you need to use the following instead, for connecting to the database 如果使用的是PDO,则需要使用以下命令来连接数据库

$db = new PDO(
          'mysql:host=localhost;dbname=data_pdo_mysql', 'username', 'password',
          array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'")
      );

After these changes, restler result should become 这些更改之后,restler结果应变为

"\u010cesk\u00fd rozhlas 1 - Radio\u017eurn\u00e1l"

Which is valid JSON with complete data where unicode characters are escaped using unicode escape sequences. 这是具有完整数据的有效JSON,其中使用Unicode转义序列对Unicode字符进行转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM