<?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;
}
?>
自定义小程序带参数获取方形和圆形二维码
最新推荐文章于 2025-06-01 23:05:18 发布
本文介绍如何在微信小程序中实现自定义参数的方形和圆形二维码生成,结合后端PHP服务,详细讲解了从接口设计到前端调用的全过程。
2072

被折叠的 条评论
为什么被折叠?



