最近做了个社交类型的小程序,这类小程序审核比较严,小程序类目得包括社交类目,而且首次审核还需要当地网监部门审核,比较费时间。信息发布必须得检测敏感内容,本地写了敏感内容过滤方法,但还是没通过,后来直接改成了小程序的检测接口。
文本内容检测接口:POST https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN
//检测内容方法
public function checkContent($title)
{
$token = $this->getToken();
$data['content'] = $title;
$url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=$token";
$info = $this->httpRequst($url,json_encode($data,JSON_UNESCAPED_UNICODE));
$result = json_decode($info,1);
if($result['errcode'] == 87014){
return 0;
}else{
return 1;
}
}
//检测图片方法
public function checkImg($path){
$token = $this->getToken();
$filePath = ROOT_PATH.'public/'.$path;
$obj = new \CURLFile(realpath($filePath));
$obj->setMimeType("image/jpeg");
$file['media'] = $obj;
$url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=$token";
$info = $this->httpRequst($url,$file);
$result = json_decode($info,1);
if($result['errcode'] == 87014){
return 0;
}else{
return 1;
}
}
//发送请求
public function httpRequst($url,$data='')
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
//获取access_token方法
public function getToken()
{
$appid = "你的appid";
$appsecret = "你的appsecret";
$filename = ROOT_PATH.'public/token.json';
if(!file_exists($filename)){
touch($filename);
}
$data = json_decode(file_get_contents($filename),true);
if (empty($data) || $data['expire_time'] < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$res = json_decode($this->httpRequst($url),true);
$access_token = $res['access_token'];
if ($access_token) {
$data['expire_time'] = time() + 7000;
$data['access_token'] = $access_token;
file_put_contents($filename, json_encode($data));
}
} else {
$access_token = $data['access_token'];
}
return $access_token;
}
特别需要注意两点:第一、content数组转换为json时json_encode必须传第二个参数JSON_UNESCAPED_UNICODE不让它转义,不然检测不出敏感词。第二、检测图片时传的图片为绝对路径