防止用戶通過自己的發佈功能發佈違規違法不良信息
需要調用微信的msgSecCheck來過濾
官方文檔添加鏈接描述
調用方法在
//內容安全
public function content_check($content)
{
$content = [
'content'=>$content
];
//小程序
$appid = '';
$secret = '';
//微信token
$token = $this->oauth2Token2($appid,$secret);
$access_token = $token->access_token;
$url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='.$access_token;
$response = $this->curl_post_weixin($url,$content);
return json_decode($response,TRUE);
}
調用api
private function curl_post_weixin($url, $data)
{
//dump(json_encode($data, JSON_UNESCAPED_UNICODE));
if($url && count($data)){
$headers = ['Content-Type'=>'application/json'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
}
被檢測的文本內容必須是json類型的數組,而且不能轉義
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
這一句是核心,普通模式json
化就只是json_encode($data)
,這種方式json
化的文本書被轉成\u***
這種格式,傳給接口的相當於是亂碼,加了參數JSON_UNESCAPED_UNICODE
就不會轉義