PHP微信开发微信授权、拉取微信个人信息

if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {//判断是否为微信环境
                if(isset($this->param['code']))//判断是否带有微信code码,没有的话则需要微信重定向
                {
                    $code = $this->param['code'];
                    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->param['appid']}&secret={$this->param['secret']}&code=".$code."&grant_type=authorization_code";//跳转到授权页面,用户同意后获取用户信息
                    $data = json_decode(file_get_contents($url),1);
                    //取得授权后,拉取用户信息
                    if($data['scope'] == 'snsapi_userinfo'){
                        //先判断有没有关注公众号
                        $acurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->param['appid']}&secret={$this->param['secret']}";//获取access_token
                        $data1 = json_decode(file_get_contents($acurl),1);
                        $access_token = $data1['access_token'];
                        $gzurl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$data['openid']."&lang=zh_CN";//根据openid和token值获取关注信息
                        $gzdata = json_decode(file_get_contents($gzurl),1);
                        if($gzdata['subscribe'] == 1){//subscribe=1表示已关注
                            $userdata = $gzdata;
                            $isFans = 1;
                        }else{
                            $userurl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$data['access_token']."&openid=".$data['openid']."&lang=zh_CN";//未关注的话,则要根据openid和token值重新获取用户信息信息
                            $userdata = json_decode(file_get_contents($userurl),1);
                            $isFans = 0;
                        }
                        
                        $openid = $userdata['openid'];
                        $nickname = $userdata['nickname'];
                        $picurl = $userdata['headimgurl'];
                    }else{//重定向
                         $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->param['appid']}&redirect_uri=http://www.jyfair.com/jxs/firstact/index?jxs_id=".$jxs_id;
                        if(isset($param['aa'])){
                            $url .= "&aa=".$param['aa'];
                        }
                        $url .= "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                        header("Location:".$url);exit;
                    }
                }else{//重定向
                    $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->param['appid']}&redirect_uri=http://www.jyfair.com/jxs/firstact/index?jxs_id=".$jxs_id;
                    if(isset($param['aa'])){
                        $url .= "&aa=".$param['aa'];
                    }
                    $url .= "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
                    header("Location:".$url);exit;
                }
        }else{//重定向
             $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6995571052457936&redirect_uri=http://www.jyfair.com/jxs/firstact/index?jxs_id=".$jxs_id;
            if($param['aa']){
                $url .= "&aa=".$param['aa'];
            }
            $url .= "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
            //print_r($url);
            header("Location:".$url);exit;
        }

你可能感兴趣的:(PHP微信开发微信授权、拉取微信个人信息)