微信小程序wx.login登陆+php

wxml:

js:

 //登录获取code  
  login: function () {
    wx.login({
      success: function (res) {
        console.log(res)        //发送请求        
        wx.request({
          url: 'http://localhost:8099/login.php', //接口地址          
          data: { code: res.code },
          success: function (res) {
            if (res.statusCode == 200) {
              console.log(res.data)
              wx.setStorageSync('openid', res.data)
            }
            else {
              console.log(res.errMsg)
            }
          },
          fail:function(e){
            console.log(e)
          }
        })
      }
    })
  },

php:

0 , "data"=>null , "errMsg"=>"error"));
    exit;
}
$code = $_GET["code"];
//配置appid
$appid = "wx8b7c8ec48bde6f27";
//配置appscret
$secret = "91dcf8013656dd5335a4a6c820cbc9f7";

$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
$info = file_get_contents($url);//get请求网址,获取数据
$jsonObj = json_decode($info);//对json数据解码
if(isset($jsonObj->errcode)){
    echo json_encode(array("statusCode"=>0 , "data"=>null , "errMsg"=>$info));
    exit;
}
$openid = $jsonObj->openid;
$session_key = $jsonObj->session_key;
echo json_encode(array("statusCode"=>0 , "data"=>$jsonObj , "errMsg"=>"success"));

?>

 

你可能感兴趣的:(微信小程序wx.login登陆+php)