最近收到了腾讯方面的警告,通知里写明了线上小程序没有进行内容安全检测,然后又开始折腾。=_=~
使用到的环境:
PHP7.3
微信第三方开发SDK:EasyWechat 4.1
文本和图片检测没什么太大区别,只是调用的方法不同(注意图片最好为绝对路径,暂时不支持网络图片,可以在图片上传的时候进行检测)
//检测文本
$result = $app->content_security->checkText($content);
//检测图片
$result = $app->content_security->checkImage('/path/to/the/image');
easywechat会自动先请求accesstoken,然后将所需要检测的内容上传到微信服务器。
// 正常返回 0
{
"errcode": "0",
"errmsg": "ok"
}
// 当文件内含有敏感内容,则返回 87014
{
"errcode": 87014,
"errmsg": "risky content"
}
下面是TP代码,写在控制器或者服务层都可以。
别忘记导入EasyWechat
use EasyWeChat\Factory;
$config = [
'app_id' => config('app_id'),
'mch_id' => config('mch_id'),
'key' => config('mch_key'),
'cert_path' => config('cert_path'),
'key_path' => config('key_path'),
'secret' => config('wx.app_secret')
];
$app = Factory::miniProgram($config);
$title_res = $app->content_security->checkText("这里传入要检测的标题");
if ($title_res['errcode'] == 87014 && $title_res['errmsg'] != "OK"){
throw new InfoException(['msg'=>'标题涉嫌违法违规']);
}
$content_res = $app->content_security->checkText("这里传入要检测的内容");
if ($content_res['errcode'] == 87014 && $content_res['errmsg'] != "OK"){
throw new InfoException(['msg'=>'内容涉嫌违法违规']);
}