QQ登录集成到自己网站php代码
我们现在在各大网站论坛都可以看到点击一个QQ图标就可以利用自己的QQ号在网站进行登录了,下面我来告诉你一段QQ登录集成到自己网站php代码,有需要的朋友可参考.
1.打开open.qq.com 添加创建应用:-》输入常规的数据,你会看到对应的APP ID和KEY值,这是对你身份证的唯一的验证.
2.打开 http://connect.qq.com/manage/ 点击->添加网站->输入相关信息,这里比较特别注意的是,回调地址那里填上你域名就可以了以上申核需要一到两天时间,耐心等待.
3.打开 http://wiki.opensns.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91%E7%BD%91%E7%AB%99%E6%8E%A5%E5%85%A5 找到SDK库下载,我在这里下载是官方的php SDK包,如果你的虚拟空间file_get_contents不技持https,可以考虑用curl函数来替代,好多童鞋常常因为获取不到access_token也就是这个原因,任何一个包不是一气呵成了,还需你配置相关的配置,这个繁索的调试过程我就在这里展开谈了,说说原理->通过你的网站登陆QQ state和scope->获得access_token->再获得每个QQ唯一的身份ID openid.
4.在这里你调试成功后,获得ID后,还需和你当前网站帐号绑定,才能下次登陆的时候自动去识别你的相关绑定帐号内容.
常见问题:
1.Warning: session_start() [function.session-start]: open(/tmpsess_s9b1ahi6vnvc0pfl2e0dcd0l10, O_RDWR) failed: 解决方法:No such file or directory (2) inD:powhostbbcarblogwebtestqqSDKcommsession.php on line 196
找到sssion.php session_save_path 加上注释//,注释掉设置路径
2.Notice: file_get_contents() [function.file-get-contents]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? inD:powhostbbcarblogwebtestqqSDKcommutils.php on line 37
解决方法:
找到 Utils.php function get_url_contents 函数内容直接用下面代码替代,因为file_get_content默认不支持访问https,如果要支持需配置php.ini,激活 php_openssl.dll 模块,这个大家百度配置一下便可,php代码如下:
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $result = curl_exec($ch);
- return $result
下面是一个完整的实例,是一个QQ登录API示范接口,程序为PHP、具体代码如下:
- <?php
-
-
-
- session_start();
- $qq_oauth_config = array(
- 'oauth_consumer_key'=>'*******',
- 'oauth_consumer_secret'=>'******************',
- 'oauth_callback'=>"http://www.sunnyi.cn/qq.php?action=reg",//这里修改为当前脚本,但是要保留?action=reg
- 'oauth_request_token_url'=>"http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token",
- 'oauth_authorize_url'=>'http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize',
- 'oauth_request_access_token_url'=>'http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token',
- 'user_info_url' => 'http://openapi.qzone.qq.com/user/get_user_info',
- );
- $action = isset($_GET['action']) ? $_GET['action'] : '';
-
-
- $qq = new qq_oauth($qq_oauth_config);
- switch($action){
-
- case 'login':
- $token = $qq->oauth_request_token();
- $_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
- $qq->authorize($token['oauth_token']);
- break;
-
- case 'reg':
- $qq->register_user();
- $access_token = $qq->request_access_token();
- if($token = $qq->save_access_token($access_token)){
-
-
- $_SESSION['oauth_token'] = $token['oauth_token'];
- $_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
- $_SESSION['openid'] = $token['openid'];
- header('Content-Type: text/html; charset=utf-8');
- $user_info = json_decode($qq->get_user_info());
- if($user_info->ret!=0){
- exit("获取头像昵称时发生错误".$user_info->msg);
- } else {
- echo 'QQ昵称:',$user_info->nickname,
- '<img src="',$user_info->figureurl,'" />',
- '<img src="',$user_info->figureurl_1,'" />',
- '<img src="',$user_info->figureurl_2,'" />';
- }
-
- }
- break;
- default :
- }
-
-
- class qq_oauth{
- private $config;
- function __construct($config){
- $this->config = $config;
- }
-
-
-
-
-
- function C($name){
- return isset($this->config[$name]) ? $this->config[$name] : FALSE;
- }
-
-
-
-
-
-
-
- function build_request_uri($url,$params=array(),$oauth_token_secret=''){
- $oauth_consumer_key = $this->C('oauth_consumer_key');
- $oauth_consumer_secret = $this->C('oauth_consumer_secret');
-
- $params = array_merge(array(
- 'oauth_version'=>'1.0',
- 'oauth_signature_method'=>'HMAC-SHA1',
- 'oauth_timestamp'=>time(),
- 'oauth_nonce'=>rand(1000,99999999),
- 'oauth_consumer_key'=>$oauth_consumer_key,
- ),$params);
- $encode_params = $params;
- ksort($encode_params);
- $oauth_signature = 'GET&'.urlencode($url).'&'.urlencode(http_build_query($encode_params));
- $oauth_signature = base64_encode(hash_hmac('sha1',$oauth_signature,$oauth_consumer_secret.'&'.$oauth_token_secret,true));
- $params['oauth_signature'] = $oauth_signature;
- return $url.'?'.http_build_query($params);
- }
-
-
-
- function check_callback(){
- if(isset($_GET['oauth_token']))
- if(isset($_GET['openid']))
- if(isset($_GET['oauth_signature']))
- if(isset($_GET['timestamp']))
- if(isset($_GET['oauth_vericode']))
- return true;
- return false;
- }
-
- function get_contents($url){
- $curl = curl_init();
- curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
- curl_setopt($curl,CURLOPT_URL,$url);
- return curl_exec($curl);
- }
-
-
-
- function oauth_request_token(){
- $url = $this->build_request_uri($this->C('oauth_request_token_url'));
- $tmp_oauth_token = $this->get_contents($url);
- parse_str($tmp_oauth_token);
-
-
-
-
-
- if(isset($error_code)) exit($error_code);
- return array(
- 'oauth_token'=>$oauth_token,
- 'oauth_token_secret'=>$oauth_token_secret
- );
- }
-
-
-
-
- function authorize($oauth_token){
- $str = "HTTP/1.1 302 Found";
- header($str);
- $url = $this->C('oauth_authorize_url');
- $query_strings = http_build_query(array(
- 'oauth_consumer_key'=>$this->C('oauth_consumer_key'),
- 'oauth_token'=>$oauth_token,
- 'oauth_callback'=>$this->C('oauth_callback'),
- ));
- header('Location: '.$url.'?'.$query_strings);
- }
-
-
-
-
- function register_user(){
-
-
-
-
-
-
-
-
-
-
- if($this->check_callback()){
-
- $signature = base64_encode(hash_hmac('sha1',$_GET['openid'].$_GET['timestamp'],$this->C('oauth_consumer_secret'),true));
- if(!emptyempty($_GET['oauth_signature']) && $signature==$_GET['oauth_signature']){
- $_SESSION['oauth_token'] = $_GET['oauth_token'];
- $_SESSION['oauth_vericode'] = $_GET['oauth_vericode'];
- return;
- }
- }
-
- exit('UNKNOW REQUEST');
- }
-
-
-
- function request_access_token(){
- $url = $this->build_request_uri($this->C('oauth_request_access_token_url'),array(
- 'oauth_token'=>$_SESSION['oauth_token'],
- 'oauth_vericode'=>$_SESSION['oauth_vericode']
- ),$_SESSION['oauth_token_secret']);
- return $this->get_contents($url);
- }
-
-
-
-
-
-
-
-
- function save_access_token($access_token_str){
- parse_str($access_token_str,$access_token_arr);
- if(isset($access_token_arr['error_code'])){
- return FALSE;
- } else {
- return $access_token_arr;
- }
- }
-
-
-
-
-
- function get_user_info(){
- $url = $this->build_request_uri($this->C('user_info_url'),array(
- 'oauth_token'=>$_SESSION['oauth_token'],
- 'openid'=>$_SESSION['openid'],
- ),$_SESSION['oauth_token_secret']);
- return $this->get_contents($url);
- }
- }?>
|