浏览Facebook API,我对正确的方法有点困惑。我希望用户跳过注册,或者在登录Facebook时自动注册。因此,如果他们登录Facebook,我会收集他们的id、电子邮件,并在我的用户表中创建一个记录。
如果用户表中已经存在id,则跳过自动注册,直接转到成员页面。这是我迄今为止的代码(取自Facebook的PHP SDK示例)。当我运行注册脚本时,页面显示为空白,我不会被重定向。
编辑:似乎在要求之后就失败了,如果我使用以下代码,"测试"永远不会打印出来。
编辑:我正在使用Codeigniter,而这个脚本是控制器的一部分,这会导致require出现问题吗?
require 'http://localhost/facebook-php-sdk-6c82b3f/src/facebook.php';
echo "test";
-
public function signup()
{
require 'http://localhost/facebook-php-sdk-6c82b3f/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user)
{
try
{
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e)
{
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user)
{
$logoutUrl = $facebook->getLogoutUrl();
}
else
{
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email'));
redirect($loginUrl);
}
print_r($user_profile);
$this->load->model("user_model");
$privileges = 1;
$loginLocation = ip2long($_SERVER['REMOTE_ADDR']);
$active = 1;
$this->user_model->add_user($user_profile->id, $user_profile->name, $user_profile->email, $loginLocation, $privileges, $active);
}
我建议您阅读框架文档。向CodeIgniter添加库不是一项艰巨的任务。脸书图书馆也不例外。
这是我刚刚提出的一个快速集成:
1.创建配置文件:application/config/facebook.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['appId'] = 'app_id';
$config['secret'] = 'app_secret';
2.将sdk文件放在库文件夹application/libraries/
中,将facebook.php
文件重命名为Facebook.php
,并将php标记替换为:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
3.在控制器中加载配置文件,然后加载Facebook库:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
$CI = & get_instance();
$CI->config->load("facebook",TRUE);
$config = $CI->config->item('facebook');
$this->load->library('facebook', $config);
}
public function index()
{
$user = $this->facebook->getUser();
if($user) {
try {
$user_info = $this->facebook->api('/me');
echo '<pre>'.htmlspecialchars(print_r($user_info, true)).'</pre>';
} catch(FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
} else {
echo "<a href='"{$this->facebook->getLoginUrl()}'">Login using Facebook</a>";
}
}
}
现在在构造函数方法中,您刚刚初始化了Facebook库(sdk),可以使用:$this->facebook
访问它。
注意:
- 你可以随时使用现有的库,只需在谷歌上搜索即可
- 一种常见的做法是扩展核心Controller类,并在那里添加Facebook库初始化
- 或者创建另一个库,扩展Facebook库,在那里加载配置文件,然后自动加载这个新库
获取$user
后,使用print_r($_SESSION)
检查会话参数。然后,用if(isset($_SESSION['some session var']))
对一些变量进行校验。如果此条件成立,则使用header("location:main.php"); exit;
导航到主页面
您可以尝试使用我的扩展http://github.com/deth4uall/Facebook-Ignited它是为了与Facebook合作而设立的。只需更新配置文件,它就可以让你做你需要做的事情,以及我自己添加的一些额外方法。
不知道为什么,但它总是运行以下行:
echo "<a href='"{$this->facebook->getLoginUrl()}'">Login using Facebook</a>";
这是错误消息:
无法连接到主机
但这奏效了。