基于PHP方法,微信公众号小程序获取code,access_token,openid,用户信息

2021-12-08 17:53:06 浏览数 (1)

//发起获得code值链接 public function doPageGetcode() { appid=′yourappid′;//修改你的appidif(!appid = 'yourappid';//修改你的appid if (!appid=′yourappid′;//修改你的appidif(!appid) { return this->result(10008, '参数错误', ''); } //这里的redirect_uri地址需要http://,跳转对于登录doPageOpenid方法,在微信公众号上面也有添加这个域名wx.owen.com/ redirect_uri=urlencode("[http://wx.owen.com/doPageOpenid](http://wx.owen.com/doPageOpenid)");redirect_uri = urlencode("[http://wx.owen.com/doPageOpenid](http://wx.owen.com/doPageOpenid)"); redirect_uri=urlencode("[http://wx.owen.com/doPageOpenid](http://wx.owen.com/doPageOpenid)");url = "open.weixin.qq.com/connect/oau…" . appid . "&redirect_uri=" . redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; header('location:' . $url); }

代码语言:javascript复制
  public function httpRequest($url, $data = null)
  {
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
     if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
     }
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     $output = curl_exec($curl);
     curl_close($curl);
     return $output;
  }
  
  //获取用户信息和注册
  public function doPageOpenid()
  {
     $appid = "yourappid";//修改你的appid
     $secret = "yourappsecret";//修改你的appsecret
     $code = $_GPC["code"];
     
     //小程序
复制代码
// url="[https://api.weixin.qq
代码语言:javascript复制
     $res = $this->httpRequest($url);
     $jsoninfo = json_decode($res, true);
     $access_token = $jsoninfo["access_token"];
     $openid = $jsoninfo["openid"];
复制代码
// // 获得所有关注用户的Openid // url="[https://api.weixin.qq.com/cgi−bin/user/get?access_token=](https://api.weixin.qq.com/cgi−bin/user/get?accesstoken=)".url = "[https://api.weixin.qq.com/cgi-bin/user/get?access_token=](https://api.weixin.qq.com/cgi-bin/user/get?access_token=)" . url="[https://api.weixin.qq.com/cgi−bin/user/get?access_token=](https://api.weixin.qq.com/cgi−bin/user/get?accesst​oken=)".access_token; // result=result = result=this->httpRequest(url);//url); // url);//jsoninfo = json_decode(result,true);//result, true); // result,true);//openid = $jsoninfo["data"]["openid"][0];
代码语言:javascript复制
     if ($access_token && $openid) {
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
        $output = $this->httpRequest($url);
复制代码
//用户信息数组jsonUserinfo jsonUserinfo=json_decode(jsonUserinfo = json_decode(jsonUserinfo=json_decode(output, true); return this−>result(0,′登录成功′,this->result(0, '登录成功', this−>result(0,′登录成功′,jsonUserinfo); } } }

0 人点赞