我正在学习如何为 facebook 创建应用程序。我想要的是一个应用程序,它在用户授权后收集用户的信息(姓名、电子邮件、生日),将其插入数据库,并在用户的墙上自动发布。
我有这段代码,但是当我运行它时,浏览器告诉我网页导致了太多重定向。
我进行了很多搜索并测试了许多不同的方法。这段代码似乎以前有效,但 facebook 改变了一些东西。我已尽力更新它而没有结果。请看一下并指导我解决它。
谢谢 这是我在根文件夹中保存为 index.php 的代码。
<?php
session_start();
if (!empty($_SESSION)) {
header("Location: index.php");
}
mysql_connect('host', 'username', 'pass');
mysql_select_db('dbname');
# require library
require("src/facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'xxxxxx',
'secret' => 'xxxxxxxx',
'cookie' => true
));
# check active session
$user = $facebook->getUser();
if (!empty($user)) {
# session active, get user id (getUser()) and user info (api->('/me'))
try {
$uid = $facebook->getUser();
$fb_access_token=$user['access_token'];
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,status_update,publish_stream,user_birthday'
));
$user = $facebook->api('/me');
$photolink = 'http://graph.facebook.com/'.$user['id'].'/picture?type=square';
$param = array(
'method' => 'users.getInfo',
'uids' => uid,
'fields' => 'pic_big'
);
$users_getinfo = $facebook->api($param);
} catch (Exception $e) {
}
if (!empty($user)) {
# active session, check if already registered the user
$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = " . $user['id']);
$result = mysql_fetch_array($query);
# If not, add it to the database
if (empty($result)) {
$query = mysql_query("INSERT INTO users (oauth_uid, oauth_provider, username, first_name, last_name, birthday, email, pic_square) VALUES ('facebook', {$user['id']}, '{$user['name']}', '{$user['first_name']}', '{$user['last_name']}', '{$user['birthday']}', '{$user['email']}', '".$photolink."')");
$query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
$result = mysql_fetch_array($query);
}
// variables in the session
$_SESSION['id'] = $result['id'];
$_SESSION['oauth_uid'] = $result['oauth_uid'];
$_SESSION['oauth_provider'] = $result['oauth_provider'];
$_SESSION['username'] = $result['username'];
} else {
# if error, kill the script
die("There was an error.");
}
$api_call = array(
'method' => 'users.hasAppPermission',
'uid' => $uid,
'ext_perm' => 'publish_stream'
);
$can_post = $facebook->api($api_call);
if ($can_post) {
# post it!
$facebook->api('/' . $uid . '/feed', 'post', array(
'message' => 'Wow you can get good prizes!',
'name' => 'Be happy',
'description' => 'Tour around the world',
'caption' => 'Win a full package around the world',
'picture' => 'http://www.test.com/test',
'link' => 'http://test.test.com/test/'
));
echo 'Posted!';
} else {
die('Permissions required!');
}
} else {
# no active session, generate one
$login_url = $facebook->getLoginUrl();
header("Location: " . $login_url);
}
?>