Question

I am trying to use graph API call to return events of a user who logged in to my app. However , $event is null all the time although I have created bunch of events myself, could anyone help?

Here is my code from login to the event api call:

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');    
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());

  // Fetch the event information
 // $events = idx($facebook->api('/me/events?limit=5'), 'data', array());
  $events = $facebook->api('/me/events','GET');
  print("11111");
  if($events == null) print("empty");
Was it helpful?

Solution

I see no login code in your example, are you sure the user is logged in? See PHP SDK "Usage": https://github.com/facebook/facebook-php-sdk

Anyway, i just tried this in one of my projects, i get the user events with the same call but i need the permission "user_events":

$facebook->getLoginUrl(array('scope' => 'user_events'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top