微信小程序中使用HTTPS調用自帶文本安全內容檢測接口 踩坑
最近php寫了一個接口使用敏感詞檢測接口(msg_sec_check) 卡了1天,不管中文傳什么內容返回的結果都是驗證通過的,
最后多方面測試發現原因是json_encode中文轉成Unicode了。
解決這個問題可以在json_encode加上參數JSON_UNESCAPED_UNICODE,禁止將中文轉Unicode
// 檢查文本內容是否違規的函數
// 發起文本安全檢測請求的函數,使用cURL發送POST請求進行文本安全檢測
function msgSecCheck($access_token){
// echo('openid' .$openid.'----');
// echo('token' .$access_token.'----');
$content = $_GET['content'];
// 3構建完整的請求URL,將access_token拼接到URL中
$url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" . urlencode($access_token);
// echo($url);
// echo('content' .$title);
// 構建文本安全檢測的請求數據
$msgSecCheckData = [
'content' => $content,
// 'openid' => $openid,
// 'version' => 2,
// 'scene' => 1
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($msgSecCheckData, JSON_UNESCAPED_UNICODE));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json'
]);
$response = curl_exec($ch);
if ($response === false) {
http_response_code(500);
$errorMsg = curl_error($ch);
curl_close($ch);
die(json_encode(['error' => 'Text security check failed. Error: ' . $errorMsg]));
}
curl_close($ch);
return json_decode($response, true);
}
如果您的問題還未解決可以聯系站長付費協助。

有問題可以加入技術QQ群一起交流學習
本站vip會員 請加入無憂模板網 VIP群(50604020) PS:加入時備注用戶名或昵稱
普通注冊會員或訪客 請加入無憂模板網 技術交流群(50604130)
客服微信號:15898888535
聲明:本站所有文章資源內容,如無特殊說明或標注,均為采集網絡資源。如若內容侵犯了原著者的合法權益,可聯系站長刪除。