php获取accesstoken和二维码的实现方法

class WeChat{
    private $_appid;
    private $_appsecret;
    private $_token;
    
    public function __construct($_appid,$_appsecret,$_token){
        $this->_appid = $_appid;
        $this->_appsecret = $_appsecret;
        $this->_token = $_token;
    }
    /**
     * 获取页面内容
     * @params $curl   URL
     * @params $https  传输协议 
     * @params $method 请求方法
     * @params $data   传输的数据
     */
    public function _request($curl,$https=true,$method='GET',$data=null){
        $ch = curl_init();//初始化
        curl_setopt($ch,CURLOPT_URL,$curl);//URL
        curl_setopt($ch,CURLOPT_HEADER,false);//
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        if($https){
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,true);    
        }
        if($method == 'POST'){
            curl_setopt($ch,CURLOPT_POST,true);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);    
        }
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
    /**
     * 获取accesstoken
     */
    public function _getAccessToken(){
        $file = './accesstoken';
        if(file_exists($file)){
            $content = file_get_contents($file);
            $content = json_decode($content);
            if(time() - filemtime($file)<$content->expires_in){
                return $content->access_token;
            }
        }
        $curl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret;
        $content = $this->_request($curl);
        file_put_contents($file,$content);
        $content = json_decode($content);
        return $content->access_token;    
    }
    /**
     * 获取二维码 1、获取ticket
     * @params $sceneid 场景id
     * @params $type    类型:临时/永久
     * @params $expire_seconds 过期时间604800 7天
     */
    public function _getTicket($sceneid,$type='temp',$expire_seconds=604800){
        if($type == 'temp'){
            $data = '{"expire_seconds": %s, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
            $data = sprintf($data,$expire_seconds,$sceneid);//格式化输出
        }else{
            $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
            $data = sprintf($data,$sceneid);
        }
        $curl = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->_getAccessToken();
        $content = $this->_request($curl,true,'POST',$data);
        $content = json_decode($content);
        return $content->ticket;
    }
    /**
     * 获取二维码 1、获取二维码QRCode
     * @params $sceneid 场景id
     * @params $type    类型:临时/永久
     * @params $expire_seconds 过期时间604800 7天
     */
    public function _getQRCode($sceneid,$type = 'temp',$expire_seconds=604800){
        $ticket = $this->_getTicket($sceneid,$type,$expire_seconds);
        $content = $this->_request('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket));
        return $content;
    }
}
$wechat = new WeChat('wxb7e7af838ec6bed2','d4624c36b6795d1d99dcf0547af5443d','');
//echo $wechat->_request("https://www.baidu.com");//获取页面内容
//echo $wechat->_getAccessToken();//获取accesstoken
header('Content-type:image/jpeg');
echo $wechat->_getQRCode(30);
If the copyright belongs to the longfei, please indicate the source!!!
原文地址:https://www.cnblogs.com/longfeiPHP/p/5119101.html