PHP 获取微信小程序链接

1.获取鉴权 Access_token

        注意:access_token 有效时长是两小时

 $app_id  = "小程序的app_id";
 $secret = "小程序sercret";
 $url = "https://api.weixin.qq.com/cgi-bin/token? 
         grant_type=client_credential&appid=$app_id&secret=$secret";
 $req = curl_init($url);
 curl_setopt($req,CURLOPT_SSL_VERIFYHOST,false);
 curl_setopt($req,CURLOPT_SSL_VERIFYPEER,false);
 curl_setopt($req, CURLOPT_RETURNTRANSFER, 1);
 $result = curl_exec($req);
 curl_close($req);
 $json = (array)json_decode($result);
 $access_token = '';
 if (isset($json['access_token'])) {
       $access_token = $json['access_token'];
 }
2.获取链接

        这块调用获取小程序的url_link,有效时长是30天,而且每个链接,只能给一个用户使用。  

  $params = array(
            "path" => "/pages/index/details",
            "query" => "id=5",   //头部参数,跳到id=5的文章
            "expire_type" => 1,
            "expire_interval" => 1,
            "env_version" => "release",
   );
   $url2 = "https://api.weixin.qq.com/wxa/generate_urllink?access_token=$access_token";
   $ch = curl_init($url2);
   curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
   curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
   $result = curl_exec($ch);
   curl_close($ch);
   $url = '';
   $json2 = (array)json_decode($result2);
   if (isset($json2['url_link'])) {
        $url = $json2['url_link'];
   }
   dump($url);

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、App/Common/Conf/db.php 数据库连接参数修改; 2、App/Api/Conf/config.php 微信小程序的appid、secret、mchid、key、notify_url,SELF_ROOT的参数修改; 3、ThinkPHP\Library\Vendor\wxpay\lib\WxPay.Config.php 微信小程序的appid、appsecret、mchid、key参数修改; 4、ThinkPHP\Library\Vendor\WeiXinpay\lib\WxPay.Config.php 微信小程序的appid、appsecret、mchid、key、notify_url参数修改; 5、App/Api/Controller/WxPayController.class.php 50行修改链接 6、后台地址:https://xxxx.xxxx.com/index.php/Admin/Login/index.html 后台登录的用户名是admin,密码是123456 这里有几个必要的条件必须得满足: 1、网站支持https; 2、小程序是用企业信息申请的小程序; 3、微信公众号已经开通微信支付; 4、小程序已经绑定微信支付; 在满足以上几个条件之后,其中就有几个非常关键的参数,任何一个出错程序运行起来都是各种问题,这几个参数分别是APPID、MCHID、KEY、APPSECRET。 1、先针对ThinkPHP\Library\Vendor\wxpay\lib\WxPay.Config.php和ThinkPHP\Library\Vendor\WeiXinpay\lib\WxPay.Config.php这两个文件里的这四个参数进行说明,首先这两个库是微信提供的,这几个参数只有填写完全正确,才可以进行正常的微信支付。 AppID是小程序ID,微信小程序后台—>设置—>开发设置便可看到; MCHID是商户号,微信支付开通的时候,会有一封来自微信的邮件,里面就有商户号; APPSECRET是APPID对应的接口密码,用于获取接口调用凭证时使用,在微信小程序后台—>设置—>开发设置便可看到; KEY是商户支付密钥(API密钥)的信息,登录微信支付商户平台,账户中心—>API安全—>API密钥 查询即可; 2、App/Api/Conf/config.php文件的参数, define(‘SELF_ROOT’,’https://xxx.xxx.com/’),修改成你自己的后台域名即可, ‘notify_url’=>’https://xxxx.xxxx.com/index.php/Api/Wxpay/notify’, xxxx也是改成你自己的后台域名, 其他appid、secret、mchid、key四个参数按以上的参数设置即可。
微信小程序可以通过node.js和微信开发者工具连接MySQL数据库。首先,需要在MySQL中配置数据库和数据表。可以使用可视化工具如Navicat for MySQL来建立数据库和数据表。然后,可以使用以下代码连接数据库并对数据进行操作: ```javascript const mysql = require('mysql'); // 创建数据库连接 const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'your_password', database: 'your_database' }); // 连接数据库 connection.connect(); // 查询数据 const sql = 'SELECT * FROM article'; connection.query(sql, (error, results, fields) => { if (error) throw error; console.log(results); }); // 关闭数据库连接 connection.end(); ``` 在微信小程序中,可以使用以上代码连接到MySQL数据库,并通过查询语句获取数据。注意,微信小程序只能对JSON格式的数据进行操作,所以需要将查询结果转换为JSON格式后再进行处理。 #### 引用[.reference_title] - *1* [微信小程序连接MySQL数据库](https://blog.csdn.net/sx17860543449/article/details/109316021)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [微信小程序PHP后端服务器数据库的连接处理](https://blog.csdn.net/fd746/article/details/113824173)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值