本文实例为大家分享了微信小程序支付PHP具体代码,供大家参考,具体内容如下
服务器端获取 openid
Getopenid.php
<?php header('Content-type: application/json; charset=UTF-8'); $APPID="";//填写小程序appid $SECRET="";//填写小程序secret $JSCODE=""; if(isset($_GET['js_code'])){ $JSCODE=$_GET['js_code']; $url="https://api.weixin.qq.com/sns/jscode2session?appid=".$APPID ."&secret=".$SECRET."&js_code=".$JSCODE."&grant_type=authorization_code"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HEADER, 0); $data = curl_exec($curl); $array=json_decode($data,true); curl_close($curl); $openid=isset($array['openid'])?$array['openid']:$array['errcode']; if($openid=="40029"){ $response["result"] = 0; $response["msg"] = "invalid code"; $response["openid"] = $openid; echo json_encode($response); }else{ $response["result"] = 1; $response["msg"] = "user exist"; $response["openid"] = $openid; echo json_encode($response); } }
小程序存储openid
在app.js中
getUserInfo:function(cb){ var that = this if(this.globalData.userInfo){ typeof cb == "function" && cb(this.globalData.userInfo) }else{ wx.login({ success: function (res) { if (res.code) { var code = res.code; wx.getUserInfo({ success: function (res2) { console.log(res2); that.globalData.userInfo = res2.userInfo; typeof cb == "function" && cb(that.globalData.userInfo) var encryptedData = encodeURIComponent(res2.encryptedData);//一定要把加密串转成URI编码 var iv = res2.iv; //请求自己的服务器 //Login(code, encryptedData, iv); wx.showToast({ title: '正在登录...', icon: 'loading', duration: 10000 }); //请求服务器 wx.request({ url: API_URL,//Getopenid.php data: { js_code: code, }, method: 'GET', header: { 'content-type': 'application/json' }, // 设置请求的 header success: function (res) { // success wx.hideToast(); console.log("JSON:" + res.data); if (res.data.result=="1"){//获取openid成功 wx.setStorage({//存储openid key: "openid", data: res.data.openid }) }else{ wx.showToast({ title: 'openid获取失败', icon: 'none', duration: 2000 }) } console.log('服务器返回' + res.data.result); console.log('服务器返回' + res.data.msg); console.log('服务器返回' + res.data.openid); }, fail: function () { // fail // wx.hideToast(); }, complete: function () { // complete } }) } }) } else { console.log('获取用户登录态失败!' + res.errMsg) } } }) } }
在登陆界面获取openid
var app = getApp()onLoad: function () { console.log('onLoad') var that = this //调用应用实例的方法获取全局数据 app.getUserInfo(function(userInfo){//获取用户信息 //更新数据 that.setData({ userInfo:userInfo }) })}
新闻热点
疑难解答