微信登录

1、微信登录类文件:
代码如下:

 

<?php
class class_app
{
    var $appid = APPID;
    var $appsecret = APPSECRET;
    var $redirecturi = REDIRECT_URI;
    var $scope = SCOPE;

    //构造函数
    public function __construct($appid = NULL, $appsecret = NULL)
    {
        if($appid && $appsecret && $redirecturi){
            $this->appid = $appid;
            $this->appsecret = $appsecret;
            $this->redirecturi = $redirecturi;
            $this->scope = $scope;
        }
    }
    
    
    //通过扫码获取code
    public function getcode()
    {
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->appid."&redirect_uri=".$this->redirecturi."&response_type=code&scope=".$this->scope."&state=STATE#wechat_redirect";
        header("Location:$url");
    }
    
    //通过微信确认登录获取code
    public function getcodephone()
    {
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$this->redirecturi."&response_type=code&scope=".$this->scope."&state=1#wechat_redirect";
        header("Location:$url");
    }
    

    //通过code获取access_token
    public function oauth2_access_token($code)
    {
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
        $res = $this->http_request($url);
        return json_decode($res, true);
    }

    //获取用户个人信息(UnionID机制)
    public function oauth2_get_user_info($access_token, $openid)
    {
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $res = $this->http_request($url);
        return json_decode($res, true);
    }

    //HTTP请求(支持HTTP/HTTPS,支持GET/POST)
    protected function http_request($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, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    //日志记录
    private function logger($log_content)
    {
        if(isset($_SERVER['HTTP_APPNAME'])){   //SAE
            sae_set_display_errors(false);
            sae_debug($log_content);
            sae_set_display_errors(true);
        }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
            $max_size = 500000;
            $log_filename = "log.xml";
            if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
            file_put_contents($log_filename, date('Y-m-d H:i:s').$log_content."\r\n", FILE_APPEND);
        }
    }
}
?>

2、PC端微信登录:
代码如下:

 

<?php
    include('./wechatlogin/1.php');
    define('APPID',            "wx3xxxaexxxxcdabxx"); //此处为微信开放平台的appid及appsecret
    define('APPSECRET',        "4ecxxxxxxxx467c04a9c3xx14axx7xx");
    define('REDIRECT_URI',     urlEncode('http://www.editbook.cn/zhaotest/indexpc.php'));
    define('SCOPE',            "snsapi_login");
    
    $weixin = new class_app();
    //传入授权临时票据(code)
    $code = $_GET['code'];
    if(!$code){
        $code = $weixin->getcode();
    }
    
    $oauth2_info = $weixin->oauth2_access_token($code);
    
    $result = $weixin->oauth2_get_user_info($oauth2_info['access_token'], $oauth2_info['openid']);
    echo '<pre/>';
    var_dump($result);

?>

PC端返回值如下:

 

array(10) {
  ["openid"]=>
  string(28) "o-CB51b0fuxxxxxXbWuy0xxxa3rw"
  ["nickname"]=>
  string(5) "Jesse"
  ["sex"]=>
  int(1)
  ["language"]=>
  string(5) "zh_CN"
  ["city"]=>
  string(0) ""
  ["province"]=>
  string(0) ""
  ["country"]=>
  string(9) "百慕大"
  ["headimgurl"]=>
  string(135) "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKnHxxxxxNiaUeZibeHTNHSiboK5ibxE6oibNUKd3Gt45UaKtRGibyQaWqYuGMYMeURXVuDvcNwWVfhixxxxx/132"
  ["privilege"]=>
  array(0) {
  }
  ["unionid"]=>
  string(28) "oSog10r-cwdBxxxxxkjixYvxxxxxI"
}

3、手机端微信登录:
代码如下:

 

<?php
    include('./wechatlogin/1.php');
    define('APPID',            "wx1dxx14xxxx7xxxdf"); //此处是公众号的appid及appsecret
    define('APPSECRET',        "5xxx97xxxx3d3xxfxxxxx73047xxxcax");
    define('REDIRECT_URI',     urlEncode('http://www.editbook.cn/zhaotest/indexphone.php'));
    define('SCOPE',            "snsapi_userinfo");
    
    $weixin = new class_app();
    //传入授权临时票据(code)
    $code = $_GET['code'];
    if(!$code){
        $code = $weixin->getcodephone();
    }
    // var_dump($code);die;
    
    $oauth2_info = $weixin->oauth2_access_token($code);
    
    $result = $weixin->oauth2_get_user_info($oauth2_info['access_token'], $oauth2_info['openid']);
    echo '<pre/>';
    var_dump($result);

?>

手机端返回值如下:

 

array(9) {
  ["openid"]=>
  string(28) "o7RXIv8xxxx1nFwMgxxxxxxxVhhE"
  ["nickname"]=>
  string(5) "Jesse"
  ["sex"]=>
  int(1)
  ["language"]=>
  string(5) "zh_CN"
  ["city"]=>
  string(0) ""
  ["province"]=>
  string(0) ""
  ["country"]=>
  string(9) "百慕大"
  ["headimgurl"]=>
  string(131) "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKx6XOxxxxxx5YfDSZEibjtSCZmAzG0ZKdXl7OZJicFBxxxxxxxWiaqOBKBHqaMSfp1DEl6C0sGFDtpBQ/132"
  ["privilege"]=>
  array(0) {
  }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值