不管用户是否已经关注,授权获取用户的基本信息

版权声明:一切都是为了学习记录,随便转载。 https://blog.csdn.net/Gjanuary/article/details/81540300

基本信息包括openid  headming 。。
思路:
  1.微信公众号后台接口权限配置吧,需要填写验证服务器的域名例如 wx.xxx.com下面的所有目录下的都能可以作为接收信息页面
  2.获取code
  3.用code拼装成授权链接即可
步骤:

1.

2.

$wxid = $_GET['wxid'];
    // 获取appid
    $data = M('userbindingwxconfig')->where('id ='.$wxid)->select();
 
    $appid = $data[0]['appId'];
 
    // 获取用户头像
    $REDIRECT_URI='http://wx.xsxsx.com/index.php?m=front&c=wxclient&a=wxauthorize&wxid='.$wxid;//回调页面    
    // $scope='snsapi_base';
    $scope='snsapi_userinfo';//需要授权
    $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".urlencode($REDIRECT_URI)."&response_type=code&scope=".$scope."&state=STATE#wechat_redirect";
    header("Location:".$url);
   3.
    public function wxauthorize(){
    	$code = $_GET['code'];
        $state = $_GET['state'];
        $wxid = $_GET['wxid'];
        $data = M('userbindingwxconfig')->where('id ='.$wxid)->select();
        $appid = $data[0]['appId'];
        $secret = $data[0]['appSecret'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
        $abs = file_get_contents($url);     
        $obj=json_decode($abs);
        $access_token = $obj->access_token;
        $openid = $obj->openid;
        $abs_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $abs_url_data = file_get_contents($abs_url);
        $obj_data=json_decode($abs_url_data,true);
       	return $obj_data;
    }

最终结果就是需要的信息了

猜你喜欢

转载自blog.csdn.net/Gjanuary/article/details/81540300