微信公众号获取openid以及access_token

    
    $appid='xxxxxxx';
    $redirect_uri = urlencode ( 'http://www.xxxxxxxx.com/xxx/xxxx/getuser' );
    $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

获取链接请求下一步操作

public function getuser()
    {//获取用户数据
        if(isset($_GET['code'])) {
           $code = $_GET["code"];
           $appId = $this->appId();
           $appSecret = $this->appSecret();

           $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appId&secret=$appSecret&code=$code&grant_type=authorization_code";
           $res = $this->sendRequest($url);
           //得到openid以及accesstoken
          $data = json_decode($res);
          $access_token = $data['access_token'];
          $openId = $data['openid'];
          $getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openId&lang=zh_CN";
          //得到用户详细参数
          $user_info = $this->sendRequest($getUserInfo);
          //进行想要的逻辑....
} 
  }
public function sendRequest($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}

  

R XG

风影OvO, 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA 4.0协议进行授权 | 转载请注明原文链接

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐

暂无内容!