Oauth授权登陆

1./D/wwwroot/site/xxx/dayrui/module/member/core/M_Controller.php

$wxinfo = $this->session->userdata('wxinfo');
if(!$wxinfo && !$this->uid){
    $state = urlencode($_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
    header('HTTP/1.1 307 Temporary Redirect');
    header('Location: '.SITE_URL.'index.php?c=oauth&state='.$state);
} elseif (!$this->uid && $wxinfo) {
    $this->member_model->wx_login($wxinfo);
}

2./D/wwwroot/site/xxx/dayrui/dayrui/controllers/Oauth.php

class Oauth extends M_Controller {
    public function __construct() {
        parent::__construct();
        $this->module['setting']['appid'] = 'xxxxx';
        $this->module['setting']['appsecret'] = 'xxxxxxx';
    }
    public function index() {
        $code = $this->input->get('code');
        $state = urlencode($this->input->get('state'));
        $redirect = urlencode('http://www.pearlshow.net/index.php?c=oauth');
        if(!$code){
            $this->_code($redirect,$state);
        } else {
           $access_token = $this->_access_token($code);
           $wxinfo = json_decode($this->_user_info($access_token));
           if(!$wxinfo->openid){
               MT_json('!$wxinfo->openid => oauth.php');
           }
           $this->session->set_userdata('wxinfo',array('openid' => $wxinfo->openid,'nickname' => $wxinfo->nickname,'headimgurl' => $wxinfo->headimgurl));
           header('HTTP/1.1 307 Temporary Redirect');
           header('Location: '.urldecode($state));            
        }
    }
    private function _code($redirect,$state) {
        header('HTTP/1.1 302 Found');
        header('Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->module['setting']['appid'].'&redirect_uri='.$redirect.'&response_type=code&scope=snsapi_userinfo&state='.$state.'#wechat_redirect');
    }
    private function _access_token($code) {
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->module['setting']['appid'].'&secret='.$this->module['setting']['appsecret'].'&code='.$code.'&grant_type=authorization_code';
        $data = json_decode(@dr_catcher_data($url), true);
        return $data['access_token'];
    }
    private function _user_info($access_token) {
        $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid=OPENID&lang=zh_CN';
        return @dr_catcher_data($url);
    }
}