PHP 实现QQ登录代码

PHP 实现QQ登录代码

废话不多说直接上代码

我将QQ登录配置信息放在login 表里面的,登录之前先检测是否有 appid , appkey , notify 回调地址。这些需要先去 QQ互联 https://connect.qq.com/ 申请,申请好通过后就有了。

//检测QQ登录配置信息
  public function qqLoginCheck()
  {
    $res = db('login')->where('id', 1)->find();
    if ($res['status'] == 0) {
      $this->error("未启用QQ登录");
    }
    if ($res['appid'] == "") {
      $this->error("QQ登录APPID不能为空");
    }
    if ($res['appkey'] == "") {
      $this->error("QQ登录APPKEY不能为空");
    }
    if ($res['notify'] == "") {
      $this->error("QQ登录回调地址不能为空");
    }
    return $res;
  }

//QQ登录
  public function qqLogin()
  {
    $bind = input('bind');
    if ($bind) {
      session('bindqq', true);
    }
    $res = $this->qqLoginCheck();
    $url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" . $res['appid'] . "&redirect_uri=" . $res['notify'] . "&scope=get_user_info&state=text";
    $this->redirect($url);
  }

  //QQ登录回调
  public function qqnotify()
  {
    $res = $this->qqLoginCheck();
    $code = $_GET['code'];
    $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=" . $res['appid'] . "&redirect_uri=" . $res['notify'] . "&client_secret=" . $res['appkey'] . "&code=" . $code;
    $response = file_get_contents($token_url);
    $params = array();
    parse_str($response, $params);
    $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" . $params['access_token'] . "&unionid=1";
    $str = file_get_contents($graph_url);
    if (strpos($str, "callback") !== false) {
      $lpos = strpos($str, "(");
      $rpos = strrpos($str, ")");
      $str = substr($str, $lpos + 1, $rpos - $lpos - 1);
    }
    $result = json_decode($str, true);
    $userinfo_url = "https://graph.qq.com/user/get_user_info?access_token=".$params['access_token']."&oauth_consumer_key=".$res['appid']."&openid=".$result['openid'];
    $userinfo = json_decode(curl_get($userinfo_url),true);
    $result = array_merge($result,$userinfo);
    
    //result 里面取得了QQ用户信息和openid等信息,下面你可以继续你的业务。
    if ($result['unionid'] != "") {
      $res = db('threepartylogin')->where('unionid', $result['unionid'])->find();
      if (empty($res)) {
        //第一次授权登录
      
      } else {
        //已授权直接登录
       
      }
    }
  }

 

import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.ImageIcon; import javax.swing.*; import java.lang.*; import java.sql.*; class Imagecanvas extends Canvas { Toolkit tool; Image Im; Imagecanvas() { setSize(360, 50); tool = getToolkit(); Im = tool.getImage("dj.jpg"); } public void paint(Graphics g) { g.drawImage(Im, 0, 0, 360, 50, this); } } class QqBorder1 extends JFrame implements ActionListener { Choice cho; QqBorder1() { super("QQ2010"); setSize(360, 260); setLocation(250, 120); setResizable(false); Container c = getContentPane(); FlowLayout layout = new FlowLayout(FlowLayout.CENTER); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); cho = new Choice(); cho.add("在线"); cho.add("隐身"); cho.add("离线"); cho.add("忙碌"); cho.add("请勿打扰"); Imagecanvas canvas1 = new Imagecanvas(); p1.setSize(360, 50); p1.add(canvas1); TextField username = new TextField(25); TextField password = new TextField(25); password.setEchoChar('*'); JButton btrystu = new JButton("登陆"); JButton sezhi = new JButton("设置"); JCheckBox check1 = new JCheckBox("记住密码"); JCheckBox check2 = new JCheckBox("自动登陆"); // p1.setSize(360, 200); JPanel p21 = new JPanel(); JPanel p22 = new JPanel(); p21.add(new JLabel("账号:")); p21.add(username); p21.add(new JLabel("<html><font color=blue>注册新账号</font></html>")); // p21.add(new JLabel(" ")); p21.add(new JLabel("密码:")); p21.add(password); p21.add(new JLabel("<html><font color=blue> 找 回 密 码 </font></html>")); p22.add(new JLabel("状态:")); p22.add(new JLabel(" ")); p22.add(cho); p22.add(check1); p22.add(check2); p22.add(new JLabel(" ")); p2.add(p21); p2.add(p22); p2.setBounds(100, 300, 200, 300); p2.setLayout(new GridLayout(2, 1, 10, 10)); p3.add(sezhi); p3.add(new JLabel(" ")); p3.add(btrystu); add(BorderLayout.NORTH, p1); add(BorderLayout.CENTER, p2); add(BorderLayout.SOUTH, p3); this.setVisible(true); } public void actionPerformed(ActionEvent e) { } } public class QqBorder { }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值