小程序接入内容内容审查接口(图片.文字)

今天不知咋回事使用easywechat的内容安全api,不知咋回事.之前还可以使用的这些天突然报这个错,也不知道是不是因为升级还是与其他的冲突,

那怎么办呢,还是用下原生的接口,在这里我获取的token方法还是easywechat的方式

$miniProgram = ZFac::miniProgram($this->config);
$token = $miniProgram->access_token->getToken()['access_token'];

具体的代码如下

小程序接入内容内容审查接口(图片.文字)_第1张图片

use EasyWeChat\Factory as ZFac;

public function __construct ( Request $request = null ){
        $this->config = [
            'app_id' => 'wx412****d45a65',
            'secret' => '082dec1*****985b93d286c093e01',
            // 下面为可选项
            // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/log/wechat.log',
            ],
        ];
}

  //文字验证
  $miniProgram = ZFac::miniProgram($this->config);
  $token = $miniProgram->access_token->getToken()['access_token'];
  // $result = $miniProgram->content_security->checkText($data['content']);
  $result = $this->z_msgSecCheck($token,$data['content']);
  if($result['errcode']!='0'){
  return jserror('内容含有非法关键词');die;
  }


//图片验证
 public function upload_pic(){
        $file = request()->file('file');
        $info = $file->validate(['ext'=>config()['web']['file_ext']])->move('./public/upload/admin/file');
        $getSaveName = str_replace('\\', '/', $info->getSaveName());//win下反斜杠替换成斜杠
        $msg = 'http://'.$_SERVER['SERVER_NAME'].'/public/upload/admin/file/'.$getSaveName;
         if(is_file('./public/upload/admin/file/'.$getSaveName)){
          //验证图片
            $miniProgram = ZFac::miniProgram($this->config);
            $token = $miniProgram->access_token->getToken()['access_token'];
            $result = $this->z_imgSecCheck($token,'./public/upload/admin/file/'.$getSaveName);
            // $miniProgram = ZFac::miniProgram($this->config);
            // $result = $miniProgram->content_security->checkImage('./public/upload/admin/file/'.$getSaveName);
            if($result['errcode']!='0'){
              return '非法图片';
            }else{
              return $msg;
            }
        }else{
            return '0';
        }
    }


核心

/*微信文字敏感内容检测*/
    public function z_msgSecCheck($token,$msg)
    {
        $checkContent = $msg;
        $url = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='. $token;
        $data = json_encode(array('content'=>$checkContent),JSON_UNESCAPED_UNICODE);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_URL,$url); // url
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // json数据
        $res = curl_exec($ch); // 返回值
        curl_close($ch);
        $result = json_decode($res,true);
        return $result;
    }
  /*微信图片敏感内容检测*/
    public function z_imgSecCheck($token,$path)
    {
        // $path = './public/upload/admin/file/'.$getSaveName;
        $url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=$token";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
        $data = array('media' => new \CURLFile(realpath($path)));
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1 );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT,"TEST");
        $result = curl_exec($curl);
        $error = curl_error($curl);
        return json_decode($result,true);
    }

高颜值后台管理系统免费使用 ### 子枫后台管理系统 ###,可在宝塔面板直接安装

欢迎关注我的公众号:子枫的奇妙世界,获得独家整理的学习资源和日常干货推送。 如果您对我的其他专题内容感兴趣,直达我的个人博客:www.wangmingchang.com 。

你可能感兴趣的:(小程序接入内容内容审查接口(图片.文字))