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
}

数据.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

我不确定 data.php 如何与 fbconnect.php 联系起来,但也许你想使用

$friends = $_SESSION['friends'];

$arr = $friends['data'];
于 2012-12-26T00:39:46.453 回答
0

据我所见,在 fbconnect.php 上,您没有启动会话。尝试插入 session_start(); 在连接()之后。

于 2012-12-26T00:50:55.740 回答