绝对使用SDK。它的好处是它是一个在野外测试和使用的库。永远不会重建轮时,你不必(你会发现,你完成更多工作;))。
我最终什么事在CI做的是Facebook的PHP SDK添加到我的图书馆目录并更改Facebook
类__construct
功能是:
public function __construct()
{
$ci =& get_instance();
$this->setAppId($ci->config->item('fb_appId'));
$this->setApiSecret($ci->config->item('fb_secret'));
$this->setCookieSupport($ci->config->item('fb_cookie'));
$this->setBaseDomain($ci->config->item('fb_domain'));
$this->setFileUploadSupport($ci->config->item('fb_upload'));
}
然后,
- 我将以上所有键/值对添加到config/config.php
- 将facebook库添加到自动载入列表
这项工作完成,我可以从任何地方通过$this->facebook
访问FB API在我的应用程序。
说了这么多,这是所有预先2.0,所以我不能完全肯定,如果需要(我使用Yii现在,这主要是为什么我不知道,如果变化是变化会是什么需要:))。
希望有所帮助。
编辑:
按照要求,我最终加入UserModel
类(扩展Model
)。我支持多个登录提供者,所以我不会发布整个事情。但是,这是它的要点:
class UserModel extends Model
{
private $m_user;
public function UserModel()
{
parent::Model();
$this->m_user = null;
$session = $this->facebook->getSession();
if($session)
{
if($this->facebook->api('/me') != null)
{
$this->m_user = $this->facebook->api('/me');
}
}
}
public function getUser()
{
return $this->m_user;
}
public function isLoggedIn()
{
return $this->getUser() != null;
}
// returns either the login or logout url for the given provider, relative to the
// state that the current user object is in
public function getActionUrl()
{
if($this->isLoggedIn())
{
return $this->facebook->getLogouturl();
}
else
{
return $this->facebook->getLoginUrl(array('next'=>currentUrl(), 'cancel'=>currentUrl(), 'req_perms'=>null, 'display'=>'popup'));
}
}
}
然后,我添加了一个登录窗口小部件,它只是包含此:
<div id="header-login">
<!-- todo: this won't work with konqueror atm (fb script bugs) - check 'em out later -->
<?php if(!$user->isLoggedIn()): ?>
<fb:login-button perms="email"><?php echo lang('fb_login'); ?></fb:login-button>
<?php else: ?>
<a onclick="FB.logout();" class="fb_button fb_button_medium"><span class="fb_button_text">Logout</span></a>
<?php endif; ?>
</div>
第二个编辑:
对不起,它是一个而自从我写这篇文章以来,所以我不得不回头弄清楚它是如何实现的:P经过快速的grep,我发现我实际上并没有在任何地方使用getActionUrl
。我添加了一些客户端脚本来收听FB登录/注销事件:
google.setOnLoadCallback(on_load);
google.load("jquery", "1.4.4");
window.fbAsyncInit = function() {
FB.init({appId: '[id]', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', on_fb_login);
FB.Event.subscribe('auth.logout', on_fb_logout);
};
function on_load()
{
// force all anchors with the rel tag "ext" to open in an external window
// (replaces target= functionality)
$('a[rel="ext"]').click(function(){
window.open(this.href);
return false;
});
}
function on_fb_login()
{
location.reload();
}
function on_fb_logout()
{
location.reload();
}
为什么不使用Facebook PHP SDK? https://github.com/facebook/php-sdk/ – 2011-03-26 02:05:43
@Russel我使用笨,不知道这将是多么容易整合。通过这种方法使用SDK有什么特别的优势吗? – 2011-03-26 02:07:29
不知道CI,但是,在Kohana中,您可以通过将其放置在供应商文件夹中来轻松添加*供应商*代码。也许CI有类似的东西。至于优点,这是一个开源,经过尝试和测试的系统,被许多人所使用。因此它将比定制的实现更强大和更安全。 – 2011-03-26 04:16:34