本文實例為大家分享了微信小程序支付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 }) })}
新聞熱點
疑難解答