簡體   English   中英

facebook應用程序代碼失敗getUser返回NULL

[英]facebook app code is failing getUser returns NULL

我正在嘗試學習Facebook應用程序開發,這是我正在嘗試的代碼。

<?php
require_once 'config.php';

/* Get a valid session */
$session = $facebook->getUser();
$me = null;
if($session) {
/* Check if session is valid */
$me = $facebook->api('/me');
}

var_dump($me);
?>

我已經在config.php中設置了我的應用程序ID和密碼,但是它無法執行getUser(),它返回的所有內容都是null。

在這里檢查。

http://facebookdevelop.byethost8.com/index.php

除了在托管站點中復制facebook php sdk之外,我是否需要進行特殊設置? 是否有地方提供最新信息,可以清楚地定義逐步說明?

沒有用戶登錄/授權,您不能僅獲取用戶ID。

看到這里: https : //developers.facebook.com/docs/reference/php/

Facebook文檔並不像很多人想象的那樣糟糕。 另一個基本教程:

https://github.com/facebook/facebook-php-sdk

請參閱“ getLoginUrl”。 抓住最新的PHP SDK,並使用git頁面上“用法”中的代碼。 “ getUser”不返回會話,它返回用戶標識(如果用戶未登錄,則返回零/零)。

此代碼有效

<?php

/**
 * This sample app is provided to kickstart your experience using Facebook's
 * resources for developers.  This sample app provides examples of several
 * key concepts, including authentication, the Graph API, and FQL (Facebook
 * Query Language). Please visit the docs at 'developers.facebook.com/docs'
 * to learn more about the resources available to you
 */

// Provides access to app specific values such as your app id and app secret.
// Defined in 'AppInfo.php'
require_once('AppInfo.php');

// Enforce https on production
if (substr(AppInfo::getUrl(), 0, 8) != 'https://' && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
  header('Location: https://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  exit();
}

// This provides access to helper functions defined in 'utils.php'
require_once('utils.php');


/*****************************************************************************
 *
 * The content below provides examples of how to fetch Facebook data using the
 * Graph API and FQL.  It uses the helper functions defined in 'utils.php' to
 * do so.  You should change this section so that it prepares all of the
 * information that you want to display to the user.
 *
 ****************************************************************************/

require_once('sdk/src/facebook.php');

$facebook = new Facebook(array(
  'appId'  => AppInfo::appID(),
  'secret' => AppInfo::appSecret(),
  'sharedSession' => true,
  'trustForwarded' => true,
));

$user_id = $facebook->getUser();
if ($user_id) {
  try {
    // Fetch the viewer's basic information
    $basic = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    // If the call fails we check if we still have a user. The user will be
    // cleared if the error is because of an invalid accesstoken
    if (!$facebook->getUser()) {
      header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
      exit();
    }
  }
  // This fetches some things that you like . 'limit=*" only returns * values.
  // To see the format of the data you are retrieving, use the "Graph API
  // Explorer" which is at https://developers.facebook.com/tools/explorer/
  $likes = idx($facebook->api('/me/likes?limit=4'), 'data', array());

  // This fetches 4 of your friends.
  $friends = idx($facebook->api('/me/friends?limit=4'), 'data', array());

  // And this returns 16 of your photos.
  $photos = idx($facebook->api('/me/photos?limit=16'), 'data', array());

  // Here is an example of a FQL call that fetches all of your friends that are
  // using this app
  $app_using_friends = $facebook->api(array(
  'method' => 'fql.query',
        'query' => 'SELECT uid, name FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1'
  ));
}

var_dump ($user_id);
?>

只需要找出區別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM