views:

66

answers:

2
 <?php

require 'facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId'  => '130407366991766',
  'secret' => 'f692cb73889096ca0835a55bc0a82c34',
  'cookie' => true,
));

// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>

<h1><a href="example.php">php-sdk</a></h1>

<?php if ($me): ?>
<a href="<?php echo $logoutUrl; ?>">
  <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif"&gt;
</a>
<?php else: ?>
<a href="<?=$loginUrl?>">
  <img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"&gt;
</a>
<?php endif ?>

<h3>Session</h3>
<?php if ($me): ?>
<pre><?php print_r($session); ?></pre>

<h3>You</h3>
<img src="https://graph.facebook.com/&lt;?php echo $uid; ?>/picture">
<?php echo $me['name']; ?>

<h3>Your User Object</h3>
<pre><?php print_r($me); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>

<h3>Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture"&gt;
<?php echo $naitik['name']; ?>

For some reason... although the login seems to be working perfectly, my script continuously states "You are not connected".

I can only assume it is a problem with my application setup. Anyone have any ideas?

Greatly Appreciated.

Demo: http://gua.com/fb-api/

A: 

Try to begin a session. Write in begin of script:

session_start ();
Alexander.Plutov
That does not work :( Thanks
Tom Clowes
PHP sessions have nothing to do with Facebook sessions.
Johannes Gorset
A: 

You need to give the Api Key to the constructor. Not the App Id.

alantto