第五节 生成微信二维码

微信的票据
1.全局票据access_token
2.网页授权access_token
3.微信js-SDK
4.jsapi_ticket

一.获取创建二维码的ticket

//获取创建二维码的ticket
    public function get_ticket(){   
        $token = new Wechat_menu_api(appID,appsecret);
        $access_token = $token->get_access_token(); 
        $url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;
        $data = array(
            'expire_seconds' => 25920,
            'action_name' => 'QR_SCENE',
            'action_info' =>
                array ('scene' => 
                    array (
                        'scene_id' => 1000
                    )
                )
            );
        $data = json_encode($data);
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $out  = curl_exec($ch);
        if(curl_errno($ch)){
            echo 'Curl error: ' . curl_error($ch);
        }
        curl_close($ch);
        $out = json_decode($out,true);
        return $out;
    }

二.获取二维码图片

//获取二维码图片
    public function get_code(){
        header('content-type: image/jpeg;charset=utf-8');
        $arr = $this->get_ticket();
        $ticket = urlencode($arr['ticket']);
        $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticket;
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
        $out = curl_exec($ch);
        if(curl_errno($ch)){
            echo 'Curl error: ' . curl_error($ch);
        }
        curl_close($ch);
        echo $out;
    }

你可能感兴趣的:(PHP微信开发)