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

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

问题描述:

I am learning how to create an app for facebook. What I want is an app that upon user authorization, collects the user's info (name, email, bday), insert it to a DB, and autopost in user's wall.

I have this code, but when I run it the browser tell me that the webpage has resulted in too many redirects.

I have searched a lot and tested many different ways. It seems that this code worked in before, but facebook changed some things. I have tried my best to update it without result. Please take a look at it and guide me to solve it.

Thanks Here is the code which I saved as index.php in my root folder.

<?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);
}

?>

You are indeed creating a loop in your code. It says that if the user has an active session, redirect him to index.php. From there he'll be redirected to index.php and so on.

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

I am not sure what you're trying to do with this redirect, but it is certainly resulting in a loop.