第三方登录示例

来源:互联网 发布:钢筋 计算 软件多少钱 编辑:程序博客网 时间:2024/05/21 09:07



  实现效果:


实现流程:


1、注册QQ互联开发者

QQ互联官网

注册成为开发者(需要审核)


2、审核通过之后

申请应用(需要互联人员审核*需要备案成功的线上域名)


下面我们开始下载QQsdk包

QQsdk包

我下载的是PHP的


下载之后放在域名根目录下

访问 域名/Connect2.1


输入appId 、appkey 、以及回调地址:域名/Connect2.1/callpack.php(也可以按照例子面试试)  一般只点击 getuserinfo


完成


优化SDK包

1、将em../auth/callbac.php 放到connect2.1目录下 将除了API目录以外的目录全部删除 并将API下的所有文件及目录移动到connect2.1/下  删除API


2、打开 qqConnectAPI.php 修改

  

[php] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. session_start();  
  3. /* PHP SDK 
  4.  * @version 2.0.0 
  5.  * @author connect@qq.com 
  6.  * @copyright © 2013, Tencent Corporation. All rights reserved. 
  7.  */  
  8.   
  9. //require_once(dirname(__FILE__)."/comm/config.php");  
  10. //修改ROOT 为QQ_CONNECT_ROOT   
  11. //修改CLASS_PATH 为 QQ_CLASS_PATH  为了避免与本地项目常量冲突  将整个个connect2.1下的全部替换  
  12.   
  13. define("QQ_CONNECT_ROOT",dirname(__FILE__)."/");  
  14. define("QQ_CLASS_PATH",QQ_CONNECT_ROOT."class/");  
  15.   
  16. require_once(QQ_CLASS_PATH."QC.class.php");  

3、删除comm 下的conf.php 以及utils.php

4.修改 class 下的Recorder.class.php 

 

[php] view plain copy print?在CODE上查看代码片派生到我的代码片
  1.     public function __construct(){  
  2.         $this->error = new ErrorCase();  
  3.   
  4.        
  5.         //将comm/下的inc.php 信息 复制过来 并删除inc.php  并删除comm目录  
  6.         $incFileContents = '{"appid":"101337757","appkey":"f6588ae8cc52093202d92f6ee33d9af9","callback":"http://www.qqq.com/callback.php","scope":"get_user_info","errorReport":true,"storageType":"file","host":"localhost","user":"root","password":"root","database":"test"}'  
  7.           
  8.          
  9.   
  10.         //-------读取配置文件  
  11.         //$incFileContents = file(ROOT."comm/inc.php");  
  12.         // $incFileContents = $incFileContents[1];  
  13.         // $this->inc = json_decode($incFileContents);  
  14.           
  15. /********/  
  16.         //将上列的contents 转位下列的模式  
  17.         $this->inc->appid = "101337757";  
  18.         $this->inc->appkey = "f6588ae8cc52093202d92f6ee33d9af9";  
  19.         $this->inc->callback = "http://www.qqq.com/callback.php";  
  20.         $this->inc->scope = "get_user_info";  
  21.         $this->inc->errorReport = true;  
  22.         $this->inc->storageType = "file"  
  23.         $this->inc->host = "localhost";  
  24.         $this->inc->user = "root";  
  25.         $this->inc->password = "root";  
  26.         $this->inc->database = "test";  
  27. /********/        
  28.           
  29.   
  30.         if(empty($this->inc)){  
  31.             $this->error->showError("20001");  
  32.         }  
  33.   
  34.         if(empty($_SESSION['QC_userData'])){  
  35.             self::$data = array();  
  36.         }else{  
  37.             self::$data = $_SESSION['QC_userData'];  
  38.         }  
  39.     }  

完成!

在根目录下定义:

function.php

[php] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <?php    
  2.   
  3. /** 
  4.  * [debug 调试输出函数] 
  5.  * @param  [mix]  $val  [调试输出源] 
  6.  * @param  [bool]  $dump [是否启用var_dump调试] 
  7.  * @param  boolean $exit [是否在调试结束后设置断点] 
  8.  * @return        [void] 
  9.  */  
  10. function debug($val,$dump=flase,$exit=true){  
  11.      //自动或区域调试函数名称$func  
  12.      if($dump){  
  13.         $func = 'var_dump';  
  14.      }else{  
  15.         $func = (is_array($val) || is_object($val)) ? 'print_r' : 'printf';  
  16.      }  
  17.   
  18.      //输出到html  
  19.      header("Content-type:text/html;charset=utf-8");  
  20.      echo "<pre>debug output:<hr/>";  
  21.      $func($val);  
  22.      echo '</pre>';  
  23.      if($exitexit;  
  24. }  
  25.   
  26.   
  27.   
  28.   
  29. ?>  

定义:qqlogin.php

[php] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <?php   
  2. require_once 'function.php';  
  3. require_once 'Connect2.1/qqConnectAPI.php';  
  4.   
  5. //访问QQ的登录页面  
  6. $oauth = new Oauth();  
  7. $oauth->qq_login();  
  8.   
  9.   
  10.   
  11.   
  12.  ?>  


建立index.html

将qq登陆图片放入页面中、点击跳入到qqlogin.php




修改callback.php

[php] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. require_once("./qqConnectAPI.php");  
  3. $qc = new QC();  
  4. echo $qc->qq_callback();  
  5. echo $qc->get_openid();  

0 0
原创粉丝点击