php mysql特殊符号过滤微信小程序_php利用小程序接口实现内容或图片敏感词过滤...

本文介绍了如何在PHP中使用微信小程序的文本和图片安全检测接口,以实现在社交类型小程序中的敏感内容过滤。通过调用微信提供的API,对发布的内容和图片进行检测,确保符合审核要求。
摘要由CSDN通过智能技术生成

最近做了个社交类型的小程序,这类小程序审核比较严,小程序类目得包括社交类目,而且首次审核还需要当地网监部门审核,比较费时间。信息发布必须得检测敏感内容,本地写了敏感内容过滤方法,但还是没通过,后来直接改成了小程序的检测接口。

文本内容检测接口: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不让它转义,不然检测不出敏感词。第二、检测图片时传的图片为绝对路径

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值