zoukankan      html  css  js  c++  java
  • PHP 实现微信小程序敏感图片、内容检测接口

    主要是为了调用微信小程序msgSecCheck、imgSecCheck接口。

    先附上小程序接口说明文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/sec-check/msgSecCheck.html
       1、首先要获取access_token(需要appId、appSecret、grant_type这个是固定值);
       
       2、文字敏感词检查接口:用获取到的access_token再去请求微信端的接口: https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN。
          这里需要注意的是: 1) message信息的格式要是JSON格式,不能直接传string,不然会报 47001,data format error hin 错误。
       
       3、图片敏感内容检查接口:用获取到的access_token再去请求微信端的接口:https://api.weixin.qq.com/wxa/img_sec_check?access_token=ACCESS_TOKEN。
          这里需要注意的是: 1) image的格式要是formdata 格式,不能直接传url,不然会报 41005,media data missing hin 错误。
                           2) 参数名应该使用:media,这是小程序定好的。
                           我这里是获取到微信上传的图片的url,然后把它下载到一个存放临时文件的区/dev/shm,然后再转为curlFile()对象 。
       
    附上具体代码:
        /*微信图片敏感内容检测*/
        public function imgSecCheck($img)
        {
            $img = file_get_contents($img);
            $filePath = '/dev/shm/tmp1.png';
            file_put_contents($filePath, $img);
            $obj = new CURLFile(realpath($filePath));
            $obj->setMimeType("image/jpeg");
            $file['media'] = $obj;
            $token = $this->getAccessToken();
            $url = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=$token";
            $info = $this->http_request($url,$file);
            return json_decode($info,true);
        }
    
        /*微信文字敏感内容检测*/
        public function msgSecCheck($msg)
        {
            $token = $this->getAccessToken();
            $url = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=$token";
            $info = $this->http_request($url,json_encode($msg));
            return json_decode($info,true);
        }
    
        /*获取access_token,不能用于获取用户信息的token*/
        public function getAccessToken()
        {
            $token_file = '/dev/shm/heka_token.json';
            $data = json_decode(file_get_contents($token_file));
            if ($data->expire_time < time()) {
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
                $res = json_decode($this->http_request($url));
                $access_token = $res->access_token;
                if ($access_token) {
                    $data->expire_time = time() + 7000;
                    $data->access_token = $access_token;
                    file_put_contents($token_file, json_encode($data));
                }
            } else {
                $access_token = $data->access_token;
            }
            return $access_token;
        }
    
    
        //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
        private function http_request($url, $data = null)
        {
            $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);
            file_put_contents('/tmp/heka_weixin.' . date("Ymd") . '.log', date('Y-m-d H:i:s') . "	" . $output . "
    ", FILE_APPEND);
            return $output;
        }
    

    转载的话:记得带上原文链接哦_ https://www.cnblogs.com/xinxinmifan/p/9722876.html

  • 相关阅读:
    4/19学习总结
    人月神话读后感8
    4/18学习总结:PullToRefresh
    构建之法阅读笔记03
    构建之法阅读笔记02
    个人总结
    大二下学期课程总结
    学习进度16
    学习进度15
    课堂测试-找英语单词最长链
  • 原文地址:https://www.cnblogs.com/xinxinmifan/p/wechat_sensitive_content_checking.html
Copyright © 2011-2022 走看看