1. 云栖社区>
  2. PHP教程>
  3. 正文

Symfony connect with facebook

作者:用户 来源:互联网 时间:2017-12-01 18:52:24

facebookactionrequest

Symfony connect with facebook - 摘要: 本文讲的是Symfony connect with facebook, routing.yml facebook_contest: url: /facebook/f param: { module: sfGuardRegister, action: facebook } actions.class.ph

routing.yml

facebook_contest:
url
: /facebook/f
param
: { module: sfGuardRegister, action: facebook }

actions.class.php

public function executeFacebookConnect(sfWebRequest $request)
{
$this->getContext()->getConfiguration()->loadHelpers('Url');
if (!$request->getParameter('code', false) && !$request->getParameter('error', false)){
//CRSF protection
$state = md5(uniqid(rand(), TRUE));
$this->getUser()->setAttribute('state', $state);
$this->redirect('https://www.facebook.com/dialog/oauth?display=popup&client_id='.sfConfig::get('app_facebook_appid').'&redirect_uri='.url_for('@facebook_connect', true).'&scope='.sfConfig::get('app_facebook_perms').'&state='.$state);
}
if ($request->getParameter('code') && $request->getParameter('state') == $this->getUser()->getAttribute('state')){//success!!
$raw = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id='.sfConfig::get('app_facebook_appid').'&redirect_uri='.url_for('@facebook_connect', true).'&client_secret='.sfConfig::get('app_facebook_appsecret').'&code='.$request->getParameter('code'));
parse_str($raw, $facebook_vars);
//get user Info with Graph
$fbuser = json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$facebook_vars['access_token']));
$sex = array('male' => '1', 'femail' => '0');
if (!empty($fbuser) && $fbuser->id){
$user = sfGuardUserTable::getInstance()->findOneBy('facebook_uid', $fbuser->id);
if (empty($user)){
$user = new sfGuardUser();
$user->setFacebookUid($fbuser->id)
->setFirstName($fbuser->first_name)
->setLastName($fbuser->last_name)
->setUsername('facebook_'.$fbuser->id)
->setSex($sex[$fbuser->gender])
->setDob(date('Y-m-d', strtotime($fbuser->birthday)))
->setSiteSection($this->getUser()->getCulture())
->setFacebookAccessToken($facebook_vars['access_token'])
->setEmailAddress($fbuser->email)
->setFacebookFather($this->getUser()->getAttribute('parrain', null))
->save();
}
else {
$user->setFacebookAccessToken($facebook_vars['access_token'])
->setEmailAddress($fbuser->email)
->setUsername('facebook_'.$fbuser->id)
->setFirstName($fbuser->first_name)
->setLastName($fbuser->last_name)
->setSex($sex[$fbuser->gender])
->setDob(date('Y-m-d', strtotime($fbuser->birthday)))
->setSiteSection($this->getUser()->getCulture())
->save();
}
$this->getUser()->setAttribute('facebook_uid', $fbuser->id);
$this->getUser()->signIn($user);
}
$this->setLayout(false);
}
}

template:

<p class="bloc-facebook">
<a class="popup" href="<?php echo url_for('@facebook_connect')?>">
<?php echo image_tag('/images/fb-connect.png', array('alt'=>'S/'inscrire avec Facebook'))?>
</a>
</p>

for the popoup:

<script type="text/javascript">
$(document).ready(
function(){
$(
'a.popup').click(function(e){
window.open($(e.currentTarget).attr(
'href'), 'share', 'width=600,height=400,directorie=no,location=no,scrollbar=no,toolbar=no');
e.preventDefault();
});
});

</script>

refresh the parent page:

in facebookConnectSuccess.php

<script type="text/javascript">
opener.location.reload();
window.close();
</script>

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索facebook , action request ,以便于您获取更多的相关知识。