Facebook应用程序:获取用户信息,插入数据库,并在墙上自动发布

时间:2012-08-17 20:05:15

标签: php mysql facebook authentication

我正在学习如何为facebook创建一个应用程序。 我想要的是一个应用程序,用户授权后,收集用户的信息(名称,电子邮件,bday),将其插入数据库,并在用户的墙上自动提供。

我有这段代码,但是当我运行它时,浏览器告诉我该网页导致了太多的重定向。

我经常搜索并测试了许多不同的方法。看来这段代码以前工作过,但是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);
}

?>

1 个答案:

答案 0 :(得分:1)

您确实在代码中创建了一个循环。它表示如果用户有活动会话,则将其重定向到index.php。从那里他将被重定向到index.php,依此类推。

if (!empty($_SESSION)) {
    header("Location: index.php");
}

我不确定你要对这个重定向做什么,但肯定会导致循环。