您不应该使用两种不同的方法来获取用户id,而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);
}
这就是你应该做的