dongliao2011 2012-12-26 00:23
浏览 66

Facebook PHP SDK会话变量未通过

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'];
}

error:

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

Is there anything I'm doing that's preventing the variable from passing?

EDIT

$_SESSION['user'] prints just fine, so I presume the problem lies with this line:

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

  • 写回答

2条回答 默认 最新

  • duanlie4621 2012-12-26 00:39
    关注

    I'm not sure how data.php ties into fbconnect.php but maybe you want to use

    $friends = $_SESSION['friends'];
    

    before

    $arr = $friends['data'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题