首先去后台新建一个develop ,
申请完成后,如果是线上测试需要开启一个开关
1.
做窗口弹出
<img οnclick="facebooklogin()" src="<?= Url::getSkinUrl("images/facebook.jpg") ?>" /><br/>
<script type="text/javascript">
var newwindow;
var intId;
function facebooklogin(){
var screenX = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft,
screenY = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth,
outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22),
width = 800,
height = 450,
left = parseInt(screenX + ((outerWidth - width) / 2), 10),
top = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
features = (
'width=' + width +
',height=' + height +
',left=' + left +
',top=' + top
);
newwindow=window.open('<?php echo $facebook_login_url; ?>','Login_by_facebook',features);
if (window.focus) {newwindow.focus()}
return false;
}
</script>
$facebook_login_url 是由下面的代码生成
1.下载集成包:php sdk 加载进来
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
public function getFacebookLoginUrl(){
session_start();
FacebookSession::setDefaultApplication( '71055737014','7b87b5d7997c7cd895abcfa5' );
$facebook = new FacebookRedirectLoginHelper("http://ecommerce.onfancy.com/facebook/account/loginv",'710521635737014','7b87b5d799fdb15eb37c7cd895abcfa5' );
$facebook_login_url = $facebook->getLoginUrl(array(
//'fbconnect' => 1,
//'canvas' => 0,
'req_perms' => 'email,publish_stream',
//'next' => 'http://ecommerce.onfancy.com/1353/fbconfig.php',
//'redirect_uri' => 'http://ecommerce.onfancy.com/1353/fbconfig.php',
));
return $facebook_login_url;
}
点击上面的图片后,直接可以访问对应的生成链接,链接大致为:
https://www.facebook.com/v2.2/dialog/oauth?client_id=710635737014&redirect_uri=http%3A%2F%2Fecommerce.onfancy.com%2Ffacebook%2Faccount%2Floginv&state=25e3cbc1f1fe6276c7b02c48a3&sdk=php-sdk-4.0.15&scope=email%2Cpublish_stream
2
然后需要编写是facebook登录成功后跳转回来的页面
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
public function actionLoginv(){
Session::set('fbs',1);
$domain = 'http://ecommerce.onfancy.com';
FacebookSession::setDefaultApplication( '7105216014','7b87b5d799fdbcd895abcfa5' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://ecommerce.onfancy.com/facebook/account/loginv' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
//echo 1;
//var_dump($session);
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
$this->accountLogin();
exit;
}else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
}
public function accountLogin(){
$fb_id = $_SESSION['FBID'];
$full_name = $_SESSION['FULLNAME'];
$email = $_SESSION['EMAIL'];
$model = Help::getModel('customer_accout');
$customer = $model->findOne(["email"=>$email]);
if(is_array($customer) && !empty($customer)){
User::Login($customer);
}else{
$name_arr = explode(" ",$full_name);
$first_name = $name_arr[0];
$last_name = $name_arr[1];
$group = User::getDefaultLoginAccountGroup();
$password = $this->getRandomPassword();
$user = [
'first_name' =>$first_name,
'last_name' =>$last_name,
'email' =>$email,
'group' =>$group,
'password' =>$password,
'create_at' =>Date::getCurrentDateTime(),
];
$model->save($user);
User::Login($user);
}
echo "<script>
window.close();
window.opener.location.reload();
</script>";
}
# 生成六位随机数
public function getRandomPassword(){
srand((double)microtime()*1000000);//create a random number feed.
$ychar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
$list = explode(",",$ychar);
for($i=0;$i<6;$i++){
$randnum=rand(0,35); // 10+26;
$authnum.=$list[$randnum];
}
//return $authnum;
return User::getEncryptPassowrd($authnum);
}
文献资料:
http://www.cnblogs.com/ouyangfang/archive/2012/02/21/2360946.html