• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

thinkPHP5 生成微信小程序二维码 保存在本地

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

PHP生成微信小程序二维码需要注意两点:

  1、小程序必须是上线的

  2、传入二维码中需要跳转的小程序页面路径最前面不能加‘/’

具体实现代码如下:

 1 <?php
 2 
 3 namespace app\admin\controller;
 4 
 5 use think\facade\Env;
 6 use app\admin\model\Company;
 7 
 8 class Index extends Common {
 9     
10     public function index() {
11         $res = 1;
12         $qrcode = self::getQrcode($res,\'pages/index/index\');
13         dump($qrcode);
14     }
15     /**
16      * 获取小程序二维码
17      * @param int $good_id                  商品ID
18      * @param varchar $samll_program_url    要跳转的路径(小程序路径)
19      */ 
20     protected static function getQrcode($good_id,$samll_program_url)
21     {
22         require_once(Env::get(\'root_path\') . \'vendor/Other/JSSDK.php\');
23         
24         $filename = "qrcode_" . $good_id . ".jpg";//要生成的图片名字
25 
26         $IMG_PATH = config(\'app.public.qrcode\');//保存服务器路径
27         $shop_url = config(\'app.public.host\');//网址
28         if (file_exists(\'./\'.$IMG_PATH .\'/\'. $filename)) { //存在返回图片路径
29             return $shop_url . $IMG_PATH .\'/\'. $filename;
30         }
31 
32         $jssdk = new \JSSDK(config(\'app.wxapp.app_id\'), config(\'app.wxapp.app_secret\'));
33         $access_token = $jssdk->getAccessTokenMinProgram();
34         $scene = $good_id;
35         $data = \'{"scene":"\' . $scene . \'","page":"\' . $samll_program_url . \'","width":280}\';
36         $jpg = posturl(\'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=\' . $access_token, $data);//二进制原始数据
37         
38         $res = file_put_contents(\'./\'.$IMG_PATH .\'/\'. $filename,$jpg);
39 
40         return $shop_url . $IMG_PATH .\'/\'. $filename;
41     }
42 }
JSSDK.php文件内容如下:
  1 <?php
  2 
  3 class JSSDK {
  4     private $appId;
  5     private $appSecret;
  6 
  7     public function __construct($appId, $appSecret) {
  8         $this->appId = $appId;
  9         $this->appSecret = $appSecret;
 10     }
 11 
 12     public function getSignPackage($url = null) {
 13         $jsapiTicket = $this->getJsApiTicket();
 14         // 注意 URL 一定要动态获取,不能 hardcode.
 15         if($url === null)
 16         {
 17             if (isset($_SERVER[\'HTTP_REFERER\'])) {
 18                 $url = $_SERVER[\'HTTP_REFERER\'];
 19             } else {
 20                 $url = \'\';
 21             }
 22         }
 23         $timestamp = time();
 24         $nonceStr = $this->createNonceStr();
 25 
 26         // 这里参数的顺序要按照 key 值 ASCII 码升序排序
 27         $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
 28 
 29         $signature = sha1($string);
 30 
 31         $signPackage = array(
 32             "app_id"     => $this->appId,
 33             "nonceStr"  => $nonceStr,
 34             "timestamp" => $timestamp,
 35             "url"       => $url,
 36             "signature" => $signature,
 37             "rawString" => $string
 38         );
 39         return $signPackage; 
 40     }
 41 
 42     private function createNonceStr($length = 16) {
 43         $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 44         $str = "";
 45         for ($i = 0; $i < $length; $i++) {
 46         $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
 47         }
 48         return $str;
 49     }
 50 
 51     private function getJsApiTicket() {
 52     
 53         $accessToken = $this->getAccessToken();
 54         // 如果是企业号用以下 URL 获取 ticket
 55         // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
 56         $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
 57         $res = json_decode($this->getUrl($url),true);
 58         if (isset($res[\'ticket\'])) {
 59             $ticket = $res[\'ticket\'];     
 60         } else {
 61             $ticket = \'\';
 62         }
 63     
 64         return $ticket;
 65     }
 66 
 67     public function getAccessToken() {
 68 
 69         if ($access_token = Cache::get("access_token")) {
 70             return $access_token;
 71         }
 72     
 73         // 如果是企业号用以下URL获取access_token
 74         // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
 75         $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
 76 
 77         $res = json_decode($this->getUrl($url),true);
 78 
 79         if (isset($res["access_token"])) {
 80             $access_token = $res["access_token"];    
 81             return $access_token;
 82         } else {        
 83             return \'\';
 84         }    
 85     
 86     }
 87 
 88     public function getAccessTokenMinProgram() {
 89 
 90         if ($access_token = Cache::get("access_token_min_program")) {
 91             return $access_token;
 92         }
 93         // 如果是企业号用以下URL获取access_token
 94         // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
 95         $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
 96         $res = json_decode($this->getUrl($url),true);
 97 
 98         if (isset($res["access_token"])) {
 99             $access_token = $res["access_token"];
100             return $access_token;
101         } else {
102             return \'\';
103         }
104 
105     }
106     private function getUrl($url){
107         $ch = curl_init();
108         curl_setopt($ch, CURLOPT_URL, $url);
109         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
110         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
111         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
112         $output = curl_exec($ch);
113         curl_close($ch);
114         return $output;
115     }
116 
117 }

注:如果file_put_contents()报错,请先检查文件夹是否创建以及是否有写入的权限;其次再检查文件路径是否正确。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PC端生成二维码,微信扫码 带参数跳转微信小程序指定页面发布时间:2022-07-18
下一篇:
小程序参数二维码自动生成发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap