微信公众号开发(三) -- 生成临时二维码

1.临时二维码和长期二维码是由 scene_id 的值区分的 scene_id=1是长期
2.通过扫描二维码关注的用户 用户信息场景值(qr_scene) 为scene_id的值
3.此处只做临时二维码的说明

//生成临时二维码
function getTimeQrCode(){
    //1、获取ticket票据
    //全局票据access_token 网页授权access_token  微信js_SDK jsapi_ticket
    $appid = ''; //微信支付申请对应的公众号的APPID
    $appsecret = ''; ////微信支付申请对应的公众号的APP Key
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
    $res = http_curl($url,'get','json');
    $access_token = $res['access_token'];
    $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
    $postArr = array(
        'expire_seconds' => 604800, //二维码过期时间 不填默认30s
        'action_name' => "QR_SCENE", //场景
        'action_info' => array(
        'scene' =>array('scene_id' => 2000,),
    ),
    );
    $postJson = json_encode($postArr);
    $res = http_curl($url,'post','json',$postJson);
    $ticket = $res['ticket'];

    //2、使用ticket获取二维码图片
    $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
    echo "<img src='".$url."' />";
}

getTimeQrCode();

/*
*$url 接口url string
*$type 请求类型 string
*$res 返回数据类型 string
*%$arr post 请求参数 string
*/
function http_curl($url,$type='get',$res='json',$arr=''){
    //1.实例化curl
    $ch = curl_init();
    //2.设置curl参数
    curl_setopt($ch,CURLOPT_URL,$url);//要访问的url地址
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//对认证证书的来源检查
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//从证书中检查SSL加密算法是否存在
    if($type=='post'){
        curl_setopt($ch, CURLOPT_POST, 1);//发送一个常规的POST请求
        curl_setopt($ch, CURLOPT_POSTFIELDS,$arr);//post提交的数据包
    }
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//获取的信息以文件流的形式返回

    //3.采集

    $output = curl_exec($ch);//执行操作
    if($res=='json'){
        if(curl_errno($ch)){
            return curl_error($ch);
        }else{
            return json_decode($output,true);
        }
    }
    //4.关闭
    curl_close($ch);
}

猜你喜欢

转载自blog.csdn.net/qq_41654694/article/details/84429247