facebook登录处理(php sdk)

首先 引入sdk

[code]
require(‘./facebook.php’);
[/code]

下面这个主要是生成登录链接:

[code lang=”php”]
public function index(){
//$this->error(‘This is just test’);
// author shanmao
//测试facebook帐号登录官网
$facebook = new Facebook(array(
‘appId’ => ‘XXXXXXXXXXXXXXXXXXXX’,
‘secret’ => ‘XXXXXXXXXXXXXX’,
‘cookie’ => true,
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who’s authenticated.
$user_profile = $facebook->api(‘/me’);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
//这是本地测试用的数据
/*
$user_profile=array(
"id" => "100003685127850",
"name" => "malasen2",
"first_name" => "Mao Mao",
"last_name" => "Zhong",
"link" => "http://www.facebook.com/profile.php?id=100003685127850",
"gender" => "male",
"email" => "malasen2@qq.com",
"locale" => "zh_CN",
"updated_time" => "2012-04-01T03:19:16+0000",
);*/

if ($user) {
$params = array( ‘next’ => ‘http://www.cityofsteam.com/cityosweb/default.php/Facebook/dofblogout’ );//处理登出
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$params = array(
‘redirect_uri’ => ‘http://www.cityofsteam.com/cityosweb/default.php/Facebook/dofbcheck’//处理登录的各种判断,如果有返回facebook用户则判断是否绑定到数据库,如果以绑定则直接登录,否则进去引导。绑定,或者注册绑定。
);
$loginUrl = $facebook->getLoginUrl($params);
}
$this->assign(‘fblogin’,$loginUrl);//模板上生成登录链接
$this->assign(‘fblogout’,$logoutUrl);
$this->display(‘test.html’);
}
[/code]

登出方法,facebook登录sdk有很严重的缓存,如果你没有清楚,一直会是已授权状态,很难处理下一步动作,这个方法之前我也没有用到。:

[code lang=”php”]
public function dofblogout(){//登出方法
$facebook = new Facebook(array(
‘appId’ => ‘xxxxxxxxxxxxxx’,
‘secret’ => ‘xxxxxxxxxxxxxxxxxxxxxxxx’,
‘cookie’ => true,
));
session_destroy();
setcookie(‘fbs_’.$facebook->getAppId(), ”, time()-100, ‘/’, ‘.cityofsteam.com’);
header(‘Location: http://www.cityofsteam.com/cityosweb/default.php/User/register’); //登出转向
}
[/code]

dofbcheck 方法后面的方法分别是 注册方法,绑定方法,由于这方面触及到登录安全,就不写了。

demo 可以到这里测试:

http://www.cityofsteam.com/cityosweb/default.php/User/register

9月10日更新(获取facebbokemail):

[code lang=”php”]
public function index(){
//$this->error(‘This is just test’);
// author shanmao
//测试facebook帐号登录官网
$facebook = new Facebook(array(
‘appId’ => ‘xxxxxxxxxxxxxxxx’,
‘secret’ => ‘xxxxxxxxxxxx’,
‘cookie’ => true,
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who’s authenticated.
$user_profile = $facebook->api(‘/me’);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
//这是本地测试用的数据
/*
$user_profile=array(
"id" => "100003685127850",
"name" => "malasen2",
"first_name" => "Mao Mao",
"last_name" => "Zhong",
"link" => "http://www.facebook.com/profile.php?id=100003685127850",
"gender" => "male",
"email" => "malasen2@qq.com",
"locale" => "zh_CN",
"updated_time" => "2012-04-01T03:19:16+0000",
);*/
//echo ‘okk’;
$params = array(
‘scope’ => ’email’,//获取facebbokemail
‘redirect_uri’ => ‘http://www.cityofsteam.com/cityosweb/default.php/Facebook/dofbcheck’//处理登录的各种判断
);
$loginUrl = $facebook->getLoginUrl($params);
if ($user) {
$params1 = array( ‘next’ => ‘http://www.cityofsteam.com/cityosweb/default.php/Facebook/dofblogout’ );//处理登出
$logoutUrl = $facebook->getLogoutUrl($params1);
}
$this->assign(‘fblogin’,$loginUrl);
$this->assign(‘fblogout’,$logoutUrl);
$this->display(‘test.html’);
}
[/code]

“facebook登录处理(php sdk)”的2个回复

评论已关闭。