1. 程式人生 > >php實現qq第三方登入

php實現qq第三方登入

     一:申請appid和appkey

網址:http://connect.qq.com/ (QQ互聯開放平臺)

 二:下載官方SDK(qqConnect_Server_SDK-php_v2.1解壓放在專案目錄下),執行install,填寫APP ID,APP KEY,callbackurl配置項。

 三:

首頁登入按鈕(可以引用官方的登入按鈕),此處我直接用a標籤跳轉

index.php

<!DOCTYPE html>
<html>
	<head>
	<meta charset="utf8">
	<meta property="qc:admins" content="1750076627657205663145770566000" />
	<title></title>
	</head>

	<body>
	<?php if(!isset($_COOKIE['qq_openid']) || !isset($_COOKIE['qq_accesstoken'])) {?>
	<a href="open.php">登入qq</a>
	<?php }else{?>
		<a href="qqlogout.php">退出qq</a>
		<?php }?>
	</body>
</html>
    open.php
require_once 'Connect2.1/qqConnectAPI.php';
    $oauth = new Oauth();
    $oauth->qq_login(); 

    qqlogout.php
<?php
     setcookie('qq_accesstoken',$accesstoken,null);
     setcookie('qq_openid',$openid,null);
     header('Location:index.php');
?>
  回撥callback.php
<?php
<span style="white-space:pre">	</span>require_once 'Connect2.1/qqConnectAPI.php';

<span style="white-space:pre">	</span>$oauth = new oauth();
<span style="white-space:pre">	</span>$accesstoken = $oauth->qq_callback();
<span style="white-space:pre">	</span>$openid=$oauth->get_openid();


<span style="white-space:pre">	</span>setcookie('qq_accesstoken',$accesstoken,time()+86400);
<span style="white-space:pre">	</span>setcookie('qq_openid',$openid,time()+86400);

<span style="white-space:pre">	</span>header('Location:index.php');
?>