编程语言
首页 > 编程语言> > php检测小程序上传图片是否违规

php检测小程序上传图片是否违规

作者:互联网

 /**
     * @Apidoc\Title("上传图片")
     * @Apidoc\Desc("上传图片")
     * @Apidoc\Url("/index/uploadpic")
     * @Apidoc\Method("get")
     * @Apidoc\Tag("")
     * @Apidoc\Header("")
     * @Apidoc\Param("filename", type="string",require=true, desc="上传图片的file名称" )
     * @Apidoc\Returned("", type="", desc="图片路径")
     */
    public function uploadpic()
    {
        $pic_path = $this->one_upload('filename');
        return $pic_path;
    }
   
     /**
     * @Apidoc\Title("图片检测")
     * @Apidoc\Desc("图片检测")
     * @Apidoc\Url("/index/imagecheck")
     * @Apidoc\Method("get")
     * @Apidoc\Tag("")
     * @Apidoc\Header("")
     * @Apidoc\Param("picpath", type="string",require=true, desc="uploadpic返回的图片路径" )
     * @Apidoc\Returned("", type="", desc="")
     */
    public function imagecheck()
    {
        $picpath=input('picpath','');
        if(empty($picpath)){ $this->BackResult(1, '请传入图片'); }
        $access_token=weixin_access_token_check();
        
        //图片检测
        $result=$this->imagecheckcall($access_token,$picpath);
        // echo '<pre>';
        // print_r($result);
       
        $this->BackResult(0, '数据结果',$result);
    }

    //图片检测
    public function imagecheckcall($access_token='',$img_path)
    {
        $img_path='./'.$img_path;
        $url='https://api.weixin.qq.com/wxa/img_sec_check?access_token='.$access_token;
        if (class_exists('\CURLFile')) {
            $josn = array('media' => new \CURLFile(realpath($img_path)));
        } else {
            $josn = array('media' => '@' . realpath($img_path));
        }
        $result=curl_call($url,'post',$josn);
        $result=json_decode($result,true);
      //print_r($result);
        return $result;
    }
 //上传图片
    public function one_upload($name)
    {
        $file = request() -> file($name);
        // echo '<pre>';
        // print_r($file);

        $saveName = Filesystem::disk('picture') -> putFile('picture', $file, time());
        $savename=str_replace('\\', '/',$saveName);
        if($savename)
        {
            return  json(array('code'=>0,'data'=>array('src'=>'/uploads/'.$savename)));
        }else{
            return  json(array('code'=>0,'msg'=>'上传失败'));
        }
    }
    
//CUrl请求调用接口
function curl_call($url,$method='get',$data='')
{
    $ch = curl_init();
    $header = array('Accept-Charset: utf-8');
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    if($error=curl_error($ch)){
        return $error;
    }
    curl_close($ch);
    return $result;
}

//微信Access_Token接口验证
function weixin_access_token_check()
{
    $info=DB::name('wx_access_token')->where(['weixin_id'=>'1'])->order('weixin_id Asc')->find();
    if(empty($info))
    {
        $result=get_weixin_access_token();
//        echo '<pre>';
//        print_r($result);
//        exit;
        if(empty($result['errcode']))
        {
            $data=array();
            $data['weixin_id']='1';
            $data['weixin_access_token']=$result['access_token'];
            $data['weixin_access_time']=time();
            DB::name('wx_access_token')->insert($data);
            return $result['access_token'];
        }
    }else{
        if(time()-$info['weixin_access_time']>=7200)
        {
            $result=get_weixin_access_token();
            if(empty($result['errcode']))
            {
                $where=array();
                $where['weixin_id']='1';

                $data=array();
                $data['weixin_id']='1';
                $data['weixin_access_token']=$result['access_token'];
                $data['weixin_access_time']=time();
                DB::name('wx_access_token')->update($data,$where);
                return $result['access_token'];
            }
        }else{
            return $info['weixin_access_token'];
        }
    }
}

function get_weixin_access_token(){
    $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.Config('app.WeiXinAPPID').'&secret='.Config('app.WeiXinAPPSECRET');
    $result=curl_call($url);
    return json_decode($result,true);
}

标签:weixin,Apidoc,access,违规,token,result,curl,php,上传
来源: https://blog.csdn.net/weixin_47164415/article/details/115031793