JSON cURL 发送的 utf8mb4 数据有错误
JSON sended by cURL has errors with utf8mb4 data
I've a script in PHP that reads from a table in DDBB, set a JSON and
send it by cURL but the endpoint receive characters like accents wrong
(acutes).
1) 首先,我连接到 bbdd 并设置字符集:
$conn=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("Error " . mysqli_error($con));
mysqli_set_charset($conn,"utf8mb4");
数据库排序规则是 utf8mb4_unicode_ci
,我无法更改它。
2) 我用以下信息构建数组:
$arr_json = array();
$array_json['key'] = htmlentities("Más");
[...]
3) 一旦确定,从数组创建JSON:
$arr_json = json_encode($arr_json,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
4) 最后,我发送信息:
curl_setopt_array($curl, array(
CURLOPT_URL => URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $arr_json,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
),
));
我收到一个 200 代码,但是 API 的输入总是收到错误的输入,例如以前的值:
'Más'
它将是:
'Más'
PHP脚本文件为UTF-8,数据库中的信息正确存储。有什么想法吗?
JSON 和 CURL 与此无关,您在此处明确转义字符:
$array_json['key'] = htmlentities("Más");
将 "Más"
变为 'Más'
。
I've a script in PHP that reads from a table in DDBB, set a JSON and send it by cURL but the endpoint receive characters like accents wrong (acutes).
1) 首先,我连接到 bbdd 并设置字符集:
$conn=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die("Error " . mysqli_error($con));
mysqli_set_charset($conn,"utf8mb4");
数据库排序规则是 utf8mb4_unicode_ci
,我无法更改它。
2) 我用以下信息构建数组:
$arr_json = array();
$array_json['key'] = htmlentities("Más");
[...]
3) 一旦确定,从数组创建JSON:
$arr_json = json_encode($arr_json,JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
4) 最后,我发送信息:
curl_setopt_array($curl, array(
CURLOPT_URL => URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $arr_json,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
),
));
我收到一个 200 代码,但是 API 的输入总是收到错误的输入,例如以前的值:
'Más'
它将是:
'Más'
PHP脚本文件为UTF-8,数据库中的信息正确存储。有什么想法吗?
JSON 和 CURL 与此无关,您在此处明确转义字符:
$array_json['key'] = htmlentities("Más");
将 "Más"
变为 'Más'
。