- extension=php_openssl.dll
- extension=php_curl.dll
- allow_url_include = on
第三步:新建配置文件
在application\config下新建文件qq_setting.php并輸入如下內(nèi)容
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * @qq互聯(lián)配置信息
- * 默認(rèn)開啟get_user_info模塊
- * **/
- $config['inc_info'] = array(
- 'appid' => '第一步中申請(qǐng)的appid',
- 'appkey' => '第一步中申請(qǐng)的key',
- 'callback' => '第一步中回調(diào)地址'
- );
第四步:創(chuàng)建自定義類
在application\libraries下新建個(gè)文件夾,命名為tencent,然后再tencent下創(chuàng)建文件oauth.php
oauth.php文件內(nèi)容如下
- <?php
- /*
- 騰訊QQ登陸模塊
- www.vxbq.cn 程序員人生
- 2015-11-19
- */
- class Oauth
- {
- public function __construct() {
- $this->access_token= '';
- $this->openid = '';
- $CI = &get_instance();
- $CI->config->load('qq_setting');
- $this->qq_set= $CI->config->item('inc_info');
- }
- //獲得登錄的 openid
- public function wget_openid($code){
- $url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id={$this->qq_set['appid']}&client_secret={$this->qq_set['appkey']}&code={$code}&redirect_uri={$this->qq_set['callback']}";
- $content=file_get_contents($url);
- if (stristr($content,'access_token=')) {
- $params = explode('&',$content);
- $tokens = explode('=',$params[0]);
- $token = $tokens[1];
- $this->access_token=$token;
- if ($token) {
- $url="https://graph.qq.com/oauth2.0/me?access_token=$token";
- $content=file_get_contents($url);
- $content=str_replace('callback( ','',$content);
- $content=str_replace(' );','',$content);
- $returns = json_decode($content);
- $openid = $returns->openid;
- $this->openid = $openid;
- $_SESSION["token2"] = $openid;
- } else {
- $openid='';
- }
- } elseif (stristr($content,'error')) {
- $openid='';
- }
- return $openid;
- }
- //用戶登陸
- public function redirect_to_login() {
- //跳轉(zhuǎn)到QQ登錄頁的接口地址, 不要更改!!
- $redirect = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id={$this->qq_set['appid']}&scope=&redirect_uri={$this->qq_set['callback']}";
- header("Location:$redirect");
- }
- /**
- * 返回用戶信息
- *
- */
- public function get_user_info(){
- $url = "https://graph.qq.com/user/get_user_info?access_token=$this->access_token&oauth_consumer_key={$this->qq_set['appid']}&openid=$this->openid";
- //$content=file_get_contents($url);
- $content=file_get_contents($url);
- $result = json_decode($content);
- return $result->nickname;
- }
- }
這個(gè)代碼,大家直接復(fù)制就可以用了
第五步:創(chuàng)建一個(gè)member控制器,在member控制器中,新建一個(gè)qq_login方法
代碼如下
- //qq用戶登陸
- public function qq_login(){
- $this->load->library('tencent/oauth','oauth');
- if(!isset($_GET['code'])){
- $this->oauth->redirect_to_login();//登陸騰訊qq,并返回到回調(diào)地址
- }else{
- $code = $_GET['code'];
- $openid = $this->oauth->wget_openid($code);
- if(!emptyempty($openid)){
- $data['info']=$this->m->get_member($openid,'connectid');//通過connectid獲取會(huì)員信息
- if(!emptyempty($data['info'])){
- //QQ已存在于數(shù)據(jù)庫(已綁定QQ的用戶),則直接轉(zhuǎn)向登陸操作
- $res=$this->m->check_login($data['info'],'qq');
- if($res==true){
- $this->message('登錄成功!',site_url($this->router->class));
- }else{
- $this->message('用戶名或密碼錯(cuò)誤,',site_url($this->router->class.'/login'));
- }
- }else{
- //未存在于數(shù)據(jù)庫中,跳去完善資料頁面。頁面預(yù)置用戶名(QQ返回是UTF8編碼,如有需要進(jìn)行轉(zhuǎn)碼)
- $user = $this->oauth->get_user_info();//獲取用戶信息
- $data['nickname'] =$user;
- $data['connectid'] = $openid;
- $this->session->set_userdata('qqsession',$data);
- $this->seotitle='用戶注冊(cè)-'.$this->seotitle;
- $this->load->view($this->router->class.'/register');
- }
- }else{
- $this->login();
- }
- }
- }
上面是我實(shí)際項(xiàng)目的方法,大家可以根據(jù)自己的項(xiàng)目修改就可以了。
第六步:放置QQ登陸圖標(biāo)
任意地方,放如下代碼就可了
- <a href="/member/qq_login" target="_blank"><img src="skin/member/images/qq.jpg"></a>
到這里,就完成了全部功能,QQ一鍵登錄用于CI框架,大家還有什么不明白的地方,都可以在程序員人生網(wǎng)給我留言