转载:ThinkPHP5 + 微信小程序 生成带参数的小程序码
前端(微信小程序)
wx.request({ url: '', // 后端接口 data: { id: 3, // 传递的参数 // path: 'pages/index/index', // 扫码进入小程序的页面路径 width: 300 // 小程序码的大小 }, success: function(res) { console.log(res.data); } })
后端(ThinkPHP5)
<?php namespace app\api\controller; class Code { // 获取accesstoken public function getAccesstoken(){ $appid = ''; // 你的小程序AppID $srcret = ''; // 你的小程序AppSecret $tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$srcret; $getArr = array(); $tokenArr = json_decode($this->send_post($tokenUrl,$getArr,"GET")); $access_token = $tokenArr->access_token; return $access_token; } public function send_post($url, $post_data,$method='POST') { $postdata = http_build_query($post_data); $options = array( 'http' => array( 'method' => $method, 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 // 超时时间(单位:s) ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } public function api_notice_increment($url, $data){ $ch = curl_init(); $header=array('Accept-Language:zh-CN','x-appkey:114816004000028','x-apsignature:933931F9124593865313864503D477035C0F6A0C551804320036A2A1C5DF38297C9A4D30BB1714EC53214BD92112FB31B4A6FAB466EEF245710CC83D840D410A7592D262B09D0A5D0FE3A2295A81F32D4C75EBD65FA846004A42248B096EDE2FEE84EDEBEBEC321C237D99483AB51235FCB900AD501C07A9CAD2F415C36DED82','x-apversion:1.0','Content-Type:application/x-www-form-urlencoded','Accept-Charset: utf-8','Accept:application/json','X-APFormat:json'); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 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_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch); if (curl_errno($ch)) { return false; }else{ return $tmpInfo; } } public function mpcode(){ $id = input('id'); $path = input('path'); $width = input('width'); // 参数 $postdata['scene'] = $id; // 宽度 $postdata['width'] = $width; // 扫码进入的页面路径 $postdata['page'] = $path; // 线条颜色 $postdata['auto_color']=false; // auto_color 为 false 时生效 $postdata['line_color']=['r'=>'0','g'=>'0','b'=>'0']; // 是否有底色为true时是透明的 $postdata['is_hyaline']=true; $post_data = json_encode($postdata); $access_token=$this->getAccesstoken(); $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token; $result=$this->api_notice_increment($url,$post_data); $data='data:image/png;base64,'.base64_encode($result); // 返回小程序码的base64 return $data; } }
PS:小程序未发布前不要传递小程序页面路径
原文路径:https://cssnb.com/index.php/archives/54/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY