php 生成小程序二维码(扫码进入小程序)

function qrCodeIsGenerated($id)
{

    $appid = config('weChat.appid');
    $secret = config('weChat.secret');
    $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
    //开启session
    session_start();
    // 保存2小时
    $lifeTime = 2 * 3600;
    setcookie(session_name(), session_id(), time() + $lifeTime, "/");
    if (empty($_SESSION['access_token'])) {
        $access_token_data = getJson($url);
        $access_token = $access_token_data['access_token'];
        $_SESSION['access_token'] = $access_token;
    }
    if (!empty($_SESSION['access_token'])) {
        $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' . $_SESSION['access_token'];
        $data['path'] = 'pages/checkList/checkList?id=' . $id;
        $data['scene'] = 'type=qrcode';//(string类型,必须是数字)
        $data['width'] = 430;
        $result = curlPost($url, $data, 'POST');
        // p($result);
        $filename = md5($id);  //文件名
        file_put_contents('./uploads/' . $filename . '.png', $result, true);
        $path = 'uploads/' . $filename . '.png';
        DB::table('archives')->where('id', $id)->update(array('wechat_image' => $path));
        return 成功($path);
    } else {
        return 失败();
    }

}



function curlPost($url,$data,$method){
    $ch = curl_init();   //1.初始化
    curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
    //4.参数如下
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https
    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'));//gzip解压内容
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

    if($method=="POST"){//5.post方式的时候添加数据
        $data = json_encode($data);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);//6.执行

    if (curl_errno($ch)) {//7.如果出错
        return curl_error($ch);
    }
    curl_close($ch);//8.关闭
    return $tmpInfo;
}



function getJson($url,$data=array(),$method='GET'){
    $ch = curl_init();//1.初始化
    curl_setopt($ch, CURLOPT_URL, $url);//2.请求地址
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
    //4.参数如下
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    if($method=="POST"){//5.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);
}

首先要拿到小城的appid 和 secret

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值