版权声明:http://www.itchuan.net 加微信zc1435661488 https://blog.csdn.net/sinat_37390744/article/details/89739559
1、注册账号 https://mp.weixin.qq.com ,获取 appID appSecret
2、配置 appID appSecret LoginUrl
$code = $_GET['code']; //此code为小程序提供
$AppID = '***';
$AppSecret = '***';
$LoginUrl = "https://api.weixin.qq.com/sns/jscode2session?" .
"appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
$wxLoginUrl = sprintf($LoginUrl,$AppID,$AppSecret,$code);
//curl
//初始化
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//不做证书校验,部署在linux上环境下需要改为true
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
$file_contents = curl_exec($curl);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
curl_close($curl);
//
$res = json_decode($file_contents,true);
附:code获取方式(小程序中)
wxml
<button type="primary" bindtap="getToken">申请令牌</button>
index.js中
var app = getApp()
var baseUrl = 'https://thinkp.cn/api/v1'; //thinkp.cn为配置的合法request域名
Page({
onLoad: function () {
},
getToken: function () {
//调用登录接口
wx.login({
success: function (res) {
var code = res.code;
console.log('code');
console.log(code); //打印出code
wx.request({
url: baseUrl + '/token/user', // 路由
data: {
code: code
},
method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
success: function (res) {
console.log(res.data);
wx.setStorageSync('token', res.data.token);
},
fail: function (res) {
console.log(res.data);
}
})
}
})
},
})