我在系统中遇到facebook用户ID的问题。通过facebook api方法getUser()
检索信息后,我无法通过对getUser()
的任何后续调用获取该ID。
以下是将用户重定向到facebook登录的注册码:
$this->view->layout ()->disableLayout ();
$this->_helper->viewRenderer->setNoRender ( true );
$appId = Ranger_Application_Util::getConfig('facebook_app_id');
$appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
require_once 'facebook/src/facebook.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true
));
$args = array();
$args['redirect_uri'] = 'http://url.com/en/account/callback-facebook';
$url = $facebook->getLoginUrl($args);
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper ( 'Redirector' );
$redirector->gotoUrl ( $url )->redirectAndExit ();
以下是重定向代码:
$this->view->layout ()->disableLayout ();
$this->_helper->viewRenderer->setNoRender ( true );
$appId = Ranger_Application_Util::getConfig('facebook_app_id');
$appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
require_once 'facebook/src/facebook.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true
));
$accessToken = $facebook->getAccessToken();
$mdl = new Core_Model_Resource_Users();
$user = $facebook->getUser();
if($user){
try {
$profile = $facebook->api('/me');
$newUserId = $mdl->insert(array(
'fuid' => $user,
'facebook_access_token' => $accessToken,
'first_name' => $profile['first_name'],
'middle_name' => $profile['middle_name'],
'last_name' => $profile['last_name'],
'status' => 'active',
'verified' => 1,
'hash' => Ranger_Application_Util::generateHash(),
'acl_role' => 'client',
'avatar' => 'default.png',
'date_created' => new Zend_Db_Expr("NOW()"),
'date_updated' => new Zend_Db_Expr("NOW()"),
'username' => 'user'.Ranger_Application_Util::generateHash()
));
} catch(Exception $e){
echo "Issue getting user information. ".$e->getMessage();
die();
}
} else {
echo "Issue getting user information";
die();
}
现在我的 zend插件 检测到facebook使用自动登录。这应该只在回调后激活。
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$appId = Ranger_Application_Util::getConfig('facebook_app_id');
$appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
require_once 'facebook/src/facebook.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true
));
$user = $facebook->getUser();
if($user){
$fetchMode = Core::db()->getFetchMode();
Core::db()->setFetchMode(Zend_Db::FETCH_OBJ);
$userProfile = $facebook->api('/me');
$mdl = new Core_Model_Resource_Users();
$sel = $mdl->select();
$sel->where('fuid=:fuid')->bind(array(":fuid"=>$user));
(OR:) $sel->where('fuid=:fuid')->bind(array(":fuid"=>$userProfile['id']));
$data = $mdl->fetchRow($sel);
Core::db()->setFetchMode($fetchMode);
Ranger_Application_Util::logUserIn($data->id);
}
无论我执行以下操作多少次:
$facebook->destroySession()
方法这是MySQL信息:
FUID -> INT(30)
FACEBOOK_ACCESS_TOKEN -> VARCHAR(255)
它仍然在检测代码部分上为$user
提供了两个不同的ID。此外,使用检测代码时ID看起来只是不等。
我应该做些什么?
答案 0 :(得分:0)
您不应该使用2种不同的方法来使用户名,并且推荐使用Graph API。 $p = $facebook->api("/me"); $p['id']
重定向代码:
$this->view->layout ()->disableLayout ();
$this->_helper->viewRenderer->setNoRender ( true );
$appId = Ranger_Application_Util::getConfig('facebook_app_id');
$appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
require_once 'facebook/src/facebook.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true
));
$accessToken = $facebook->getAccessToken();
$mdl = new Core_Model_Resource_Users();
$user = $facebook->getUser();
if($user){
try {
$profile = $facebook->api('/me');
$newUserId = $mdl->insert(array(
'fuid' => $profile["id"],
'facebook_access_token' => $accessToken,
'first_name' => $profile['first_name'],
'middle_name' => $profile['middle_name'],
'last_name' => $profile['last_name'],
'status' => 'active',
'verified' => 1,
'hash' => Ranger_Application_Util::generateHash(),
'acl_role' => 'client',
'avatar' => 'default.png',
'date_created' => new Zend_Db_Expr("NOW()"),
'date_updated' => new Zend_Db_Expr("NOW()"),
'username' => 'user'.Ranger_Application_Util::generateHash()
));
} catch(Exception $e){
echo "Issue getting user information. ".$e->getMessage();
die();
}
} else {
echo "Issue getting user information";
die();
}
再次确保fuid
列设置为字符串类型MySQL varchar(200)
是我用于Facebook UID的
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$appId = Ranger_Application_Util::getConfig('facebook_app_id');
$appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
require_once 'facebook/src/facebook.php';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'cookie' => true
));
$user = $facebook->getUser();
if($user){
$fetchMode = Core::db()->getFetchMode();
Core::db()->setFetchMode(Zend_Db::FETCH_OBJ);
$userProfile = $facebook->api('/me');
$mdl = new Core_Model_Resource_Users();
$sel = $mdl->select();
$sel->where('fuid='.$userProfile['id']);
// i don't like bind for one variable php will be quicker using string concatenation
$data = $mdl->fetchRow($sel);
Core::db()->setFetchMode($fetchMode);
Ranger_Application_Util::logUserIn($data->id);
}
这就是你应该做的一切