微信小程序内容安全校验----PHP代码

微信小程序内容安全(已测试)

最近我开发项目遇到用户发布文本内容和图片需要后台审核,这是防止用户发一些违禁内容让我们平台受到影响。但是后台人为审核严重影响用户使用和体验,因此我查了微信文档就封装这两个方法,这样就不需要后台审核了,微信小程序自动审核。这样用户体验上升。注意new\CURLFile();对于PHP5.5以上的,PHP自带的。因为我用的是TP3.2,所以用框架写,希望对大家有帮助

(1)校验一张图片是否含有违法违规内容   

PHP(TP3.2代码)

//检测图片是否含有违禁内容(频率限制:单个 appId 调用上限为 1000 次/分钟,100,000 次/天)图片尺寸不超过 750px * 1334px
private function curlImg($path){
    $token=M('config')->field('access_token')->where('1=1')->find();
    $ACCESS_TOKEN=$token['access_token'];//自己获取access_token
    $url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=".$ACCESS_TOKEN;
   // $file_path =realpath("24.jpg");
    $file_path = $_SERVER['DOCUMENT_ROOT']."/".$path;//接受表单文件保存本地的文件地址
    //$file_path = DOC_ROOT . "/Public/Uploads/paimai_goods/2018-06-14/tmp_71012dc7fc3e63ad1f3e9950ffb43bca394d88ecae3798d6.png";
    $file_data = array("media"  => new \CURLFile($file_path));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch , CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $file_data);
    $output = curl_exec($ch);//发送请求获取结果
    curl_close($ch);//关闭会话
    $output=json_decode($output,ture);
    return $output['errmsg'];//返回结果
}

(2)检查一段文本是否含有违法违规内容

PHP(TP3.2代码)

//检测文本是否含有违禁内容(频率限制:单个 appId 调用上限为 1000 次/分钟,100,000 次/天)
private function curlText($content){
    $token=M('config')->field('access_token')->where('1=1')->find();
    $ACCESS_TOKEN=$token['access_token'];//自己获取access_token
    $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$ACCESS_TOKEN;
    // $content="hello world!";
    $file_data = '{ "content":"'.$content.'" }';//$content(需要检测的文本内容,最大520KB)
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch , CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $file_data);
    $output = curl_exec($ch);//发送请求获取结果
    curl_close($ch);//关闭会话
    $output=json_decode($output,ture);
    return $output['errmsg'];//返回结果
}

猜你喜欢

转载自blog.csdn.net/weixin_37616043/article/details/80700962
今日推荐