php生成小程序任意页面二维码,生成小程序带参数路由二维码

背景:在小程序内做分享功能需要实时生成当前页面二维码

<?php
header("Content-Type: text/html; charset=utf-8");
function wxcode($code){  
    $appid = '';
    $secret = '';
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
    session_start();
    $lifeTime = 2 * 3600;
    setcookie(session_name(), session_id(), time() + $lifeTime, "/");
    $access_token = $_SESSION['access_token'];
    if(empty($access_token)){
        $access_token_data = getJson($url);
        $access_token = $access_token_data['access_token'];
        $_SESSION['access_token'] = $access_token;
    }
    if(!empty($access_token)){
        $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
        //用户扫码访问小程序路径
        $data['path'] = 'pages/player/index?id='.$code;
        $data['scene'] = 'type=qrcode';
        $data['width'] = 430;
        $result = curlPost($url,$data,'POST');
        $filename = md5($code); 
        $ret = file_put_contents('./uploads/'.$filename.'.png', $result, true);
        $path='./uploads/'.$filename.'.png';
        //输出生成二维码路径
        echo $path;
        echo '成功';
    }else{
        echo 'string';
    }
}
function getJson($url,$data=array(),$method='GET'){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    if($method=="POST"){
        $data = json_encode($data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}

function curlPost($url,$data,$method){
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    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_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

    if($method=="POST"){
        $data = json_encode($data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);

    if (curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_close($ch);
    return $tmpInfo;
}
wxcode(62);
?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G佳伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值