yii开发, Facebook api 互联登录,请教一下具体流程?
------解决方案--------------------
facebook session获取 sdk会做,不用自己写。
sdk应该包含以下三个文件
base_facebook.php
facebook.php
fb_ca_chain_bundle.crt
<?php
include "facebook.php";
$oFacebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true,
'fileUpload' => true
));
$user = $oFacebook->getUser();
if($user){
try{
$user_profile = $oFacebook->api('/'.$user); //判斷是否獲取到用戶信息
} catch (FacebookApiException $e){
$user = null;
}
if($user){
return $user; //返回用戶id
}
}else{
$loginUrl = $oFacebook->getLoginUrl(array(
'scope' => 'xxxx', // 这里填权限,要获取的东西 例如 publish_stream,user_photos,email,offline_access,user_birthday,user_notes
'redirect_uri' => 'xxx' // 这里填在facebook login后要回跳的地址
));
return $loginUrl;
}
?>
得到$loginUrl后,跳转到这个地址
当facebook 登入成功后,会返回到 redirect_uri这个地址,并把参数带回
redirect_uri php可以这样写
<?php
$userid = $oFacebook->getUser();
$facebook_accesstoken = $facebook->getAccessToken();
echo $facebook_accesstoken.'<br>';
try{
$param = array(
'method' => 'fql.query',
'query' => "SELECT 'uid,name,pic,pic_square,email,sex,birthday_date' FROM user WHERE uid='".$userid."'",
'access_token' => $facebook_accesstoken
);
$result = oFacebook->api($param);
} catch(FacebookApiException $e){
return array();
}
if($result){
$user_profile = array();
foreach($result[0] as $key=>$val){
$user_profile[$key] = $val;
}
return $user_profile;
}else{
return array();
}
print_r($result);
?>
剩下的就是自己服务器那边的操作了。