0
votes

I am trying to make a php facebook app. Downloaded the Facebook php sdk and used the example.php (with my own appId and secret) to try to get a example app running. I can use the example app, but when I click login with facebook and and authenticate the app it throws this exception:

"Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /var/www/facebook_test/base_facebook.php on line 1264"

Below is the code I am using.

Any guidance would be greatly appreciated.

<?php
require 'facebook.php';
$facebook = new Facebook(array(
  'appId'  => 'myappid',
  'secret' => 'mysecretid',
  'cookie' => true,
));

// Get User ID
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } 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 {
  $statusUrl = $facebook->getLoginStatusUrl();
  $loginUrl = $facebook->getLoginUrl();
}

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

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Check the login status using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo urldecode($statusUrl); ?>">Check the login status</a>
      </div>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo urldecode($loginUrl); ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

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

    <h3>Public profile of Naitik</h3>
    <img src="https://graph.facebook.com/naitik/picture">
    <?php echo $naitik['name']; ?>
  </body>
</html>

Edit: This is the stack trace:

#0 /var/www/facebook_test/base_facebook.php(873): 
BaseFacebook->throwAPIException(Array) #1 [internal function]: 
BaseFacebook->_graph('/naitik') #2 /var/www/facebook_test/base_facebook.php(647): 
call_user_func_array(Array, Array) #3 /var/www/facebook_test/index.php(66): 
BaseFacebook->api('/naitik') #4 {main}
1
Can you show us the full stacktrace? (ie. catch the error) - Halcyon
The "Fatal error ..." message is what is displayed in the browser. How can I get a full stacktrace? Sorry, but this is the first time I'm developing a php web application. - Jea
It's a Fatal error because an exception is thrown that you didn't catch. Put a try-catch around the part that throws the exception. Then you can $e->getTraceAsString(); - Halcyon
Okay, I'll try it and do an edit of this post afterwards. - Jea
Now I did an update with the stack trace. - Jea

1 Answers

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

Apparently it doesn't!