自定义小程序带参数获取方形和圆形二维码

本文介绍如何在微信小程序中实现自定义参数的方形和圆形二维码生成,结合后端PHP服务,详细讲解了从接口设计到前端调用的全过程。
<?php
header('content-type:text/html;charset=utf-8');
define("ViaGe_Weixin_Access_Token",str_replace('\\','/',dirname(__FILE__)));//根目录物理路径
//配置APPID、APPSECRET
$appid = "APPID";
$secret = "APPSECRET";
$qrcode=_qrcode("1",$appid,$secret,"258");
echo '<img src="./'.$qrcode['img'].'" >';
//获取access_token
function access_token($appid,$secret){
    $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
    $access_path = ViaGe_Weixin_Access_Token."/" . $appid . ".json";
    $file = file_get_contents($access_path, true);
    $access = json_decode($file, true);
    if (time() > $access['expires']) {
        $get_access_token = Roboa_Url($access_token);
        $get_access_token = json_decode($get_access_token, true);
        $data = array();
        $data['access_token'] = $get_access_token['access_token'];
        $data['expires'] = time() + 7200;
        $jsonStr = json_encode($data);
        $fp = fopen($access_path, "w");
        fwrite($fp, $jsonStr);
        fclose($fp);
        return $data['access_token'];
    } else {
        return $access['access_token'];
    }
}
function _qrcode($type,$appid,$secret,$uid) {
    $width='430';
    switch ($type){
        case "1"://获取圆形小程序图像
            $access_token = $access_token =access_token($appid,$secret);;
            $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
            $data['scene'] = 'roboa_token='.$uid;
            $data['path'] = 'pages/index/index';
            $data['width'] = $width;
            $res = Roboa_Url($url, json_encode($data),'POST');
            $path = ViaGe_Weixin_Access_Token."/".$uid."qrcode.png";
            $imgpath=$uid."qrcode.png";
            file_put_contents($path, $res);
            $return['status_code'] = 2000;
            $return['msg'] = 'ok';
            $return['img'] = $imgpath;
            $flag['img'] = $return['img'];
            break;
        case "2"://获取方形小程序图像
            $access_token =access_token($appid,$secret);
            $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$access_token";
            $param = json_encode(array("path" => "pages/index/index?roboa_token=".$uid, "width" => $width));
            $result = Roboa_Url($qcode, $param, "POST");
            file_put_contents(ViaGe_Weixin_Access_Token."/".$uid."qrcode.png", $result);
            $imgpath=$uid."qrcode.png";
            $flag['img']= $imgpath;
            break;
    }
    return $flag;
}
function Roboa_Url($url, $data = '', $method = 'GET'){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    if ($method == 'POST') {
        curl_setopt($curl, CURLOPT_POST, 1);
        if ($data != '') {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
    }
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}
?>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值