1

fbconnect.php

include('functions.php');
connect();

require 'src/facebook.php';

if (!isset($_SESSION['user'])) {

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'xxx',
  'secret' => 'xxx',
));

// 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', 'GET');

$ret = $facebook->api( array(
                         'method' => 'fql.query',
                         'query' => 'SELECT affiliations FROM user WHERE uid = me()',
                     ));

$network = $ret['0']['affiliations']['0']['name'];
//echo $network;

$data = $facebook->api('/me', 'get', array("fields"=>"location"));
$location = $data['location']['name'];

$sql = mysql_query("SELECT * FROM xx_users WHERE user_id=$user_profile[id]");

if (mysql_num_rows($sql) == 0) {


mysql_query("INSERT INTO xx_users (name, user_id, email, network, location)
VALUES ('$user_profile[name]', '$user_profile[id]', '$user_profile[email]', '$network', '$location')");

}

else {


}


  } 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();
$loginUrl = $facebook->getLoginUrl(array(
        'scope'     => 'email, user_location, user_work_history, user_education_history', // Permissions to request from the user
        'redirect_uri'  => 'http://comfyshoulderrest.com/socialexchange/home.php', // URL to redirect the user to once the login/authorization process is complete.
        ));

}
}

else {
$_SESSION['user'] = $user;
$_SESSION['friends'] = $facebook->api('/me/friends');
// $_SESSION['friends'] = $facebook->api('/$user/friends'); THIS GIVES THE SAME RESULT
}

data.php

include('functions.php');
connect();
session_start();

$friends = $_SESSION['friends'];
$user = $_SESSION['user'];

$arr = $friends['data'];
$friend_ids_arr = array();
foreach ($arr as $friend) { // THIS IS LINE 14
    $friend_ids_arr[] = $friend['id'];
}

エラー:

Warning: Invalid argument supplied for foreach() in /blah/data.php on line 14

変数の通過を妨げていることはありますか?

編集

$_SESSION['user']問題なく印刷されるので、問題は次の行にあると思います。

$_SESSION['friends'] = $facebook->api('/me/friends');

4

2 に答える 2

0

私が見たところ、fbconnect.phpではセッションを開始していません。session_start();を挿入してみてください。connect()の後。

于 2012-12-26T00:50:55.740 に答える
0

data.php が fbconnect.php にどのように結び付いているかはわかりませんが、使用したいかもしれません

$friends = $_SESSION['friends'];

$arr = $friends['data'];
于 2012-12-26T00:39:46.453 に答える