一、说明

1、这里讲解生成二维码接口(临时二维码和永久二维码)
2、这里给出简单的实现代码,不做详细说明
3、可以参考开发手册:
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542
4、参考视频:
https://www.imooc.com/video/11357

二、代码

    /*
    *$url 接口url string
    *$type 请求类型 string
    *$res 返回数据类型 string
    *%$arr post 请求参数 string
    */
    public function http_curl($url,$type='get',$res='json',$arr=''){
        //1.初始化curl
        $ch = curl_init();
        //2.设置curl的参数
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if($type == 'post'){
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
        }
        //3.采集
        $output = curl_exec($ch);
        //4.关闭
        curl_close($ch);
        if($res=='json'){
            if(curl_error($ch)){
                //请求失败,返回错误信息
                return curl_error($ch);
            }else{
                //请求成功,返回错误信息
                return json_decode($output,true);
            }
        }
    }
    /*
    *返回access_token *session解决办法 存mysql memcache
    */
    public function getWxAccessToken(){
        //将access_token 存在session/cookie中
        if($_SESSION['access_token'] && $_SESSION['expire_time'] > time()){
            //如果access_token在session中没有过期
            return $_SESSION['access_token'];
        }else{
            //如果access_token在session中不存在或者已经过期
            $appid = 'wxb20bebb764546087';
            $appsecret = '64e22d215895d52af21c8fcbf6da3241';
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
            $res = $this->http_curl($url,'get','json');
            $access_token = $res['access_token'];
            //将重新获取到的access_token存到session中
            $_SESSION['access_token'] = $access_token;
            $_SESSION['erpire_time'] = time() + 7000;
            return $access_token;
        }
    }
//生成临时二维码
    public function getTimeQrCode(){
        //1、获取ticket票据
        //全局票据access_token 网页授权access_token  微信js_SDK jsapi_ticket
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
        $postArr = array(
            'expire_seconds' => 604800,
            'action_name' => "QR_SCENE", 
            'action_info' => array(
                    'scene' =>array('scene_id' => 2000),
            ),
        );
        $postJson = json_encode($postArr);
        $res = $this->http_curl($url,'post','json',$postJson);
        //var_dump($res);
        $ticket = $res['ticket'];
        //var_dump($ticket);
        //2、使用ticket获取二维码图片
        $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
        echo "<img src='".$url."' />";
    }
    //生成永久二维码
    public function getForeverQrCode(){
        //1、获取ticket票据
        //全局票据access_token 网页授权access_token  微信js_SDK jsapi_ticket
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
        $postArr = array(
            'action_name' => "QR_LIMIT_SCENE", 
            'action_info' => array(
                    'scene' =>array('scene_id' => 3000),
            ),
        );
        $postJson = json_encode($postArr);
        $res = $this->http_curl($url,'post','json',$postJson);
        //var_dump($res);
        $ticket = $res['ticket'];
        //var_dump($ticket);
        //2、使用ticket获取二维码图片
        $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
        echo "<img src='".$url."' />";
    }

本文由 一切随风 创作,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论