PHP 实现微信小程序敏感图片、内容检测接口

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

        先附上小程序接口说明文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/sec-check/msgSecCheck.html
        1、首先要获取access_token(需要appId、appSecret、grant_type这个是固定值);
        ###msgSecCheck接口:
        2、用获取到的token带入到微信端的接口地址: https://api.weixin.qq.com/wxa/msg_sec_check?access_token=ACCESS_TOKEN。
            这里需要注意的是: 1、message信息的格式要是JSON格式,不能直接传string,不然会报 47001,data format error hin 错误。
       ###imgSecCheck接口:
        3、用获取到的token带入到微信端的接口地址:https://api.weixin.qq.com/wxa/img_sec_check?access_token=ACCESS_TOKEN。
            这里需要注意的是: 1、image的格式要是formdata 格式,不能直接传url,不然会报 41005,media data missing hin 错误。参数名应该使用: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') . "\t" . $output . "\n", FILE_APPEND);
        return $output;
    }

猜你喜欢

转载自www.cnblogs.com/xinxinmifan/p/9722876.html