0

I have followed the Facebook PHP SDK example and created the following.

<?php

    require ("lib/facebook.php");

    $facebook = new Facebook(array('appId' => 'XXXXXXXXXXXX', 'secret' => 'XXXXXXXXXXXXXXXXXXXXXXX', 'cookie' => true, ));

    $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();
    }
    ?>

    <!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>
    <div id="fb-root"></div>
    <script>
          window.fbAsyncInit = function() {
            FB.init({
              appId   : '<?php
    echo $facebook->getAppId();
    ?>',
              session : <?php
    echo json_encode($session);
    ?>, // don't refetch the session when PHP already has it
              status  : true, // check login status
              cookie  : true, // enable cookies to allow the server to access the session
              xfbml   : true // parse XFBML
            });

            // whenever the user logs in, we refresh the page
            FB.Event.subscribe('auth.login', function() {
              window.location.reload();
            });
          };

          (function() {
            var e = document.createElement('script');
            e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
            e.async = true;
            document.getElementById('fb-root').appendChild(e);
          }());
        </script>
    <?php
    if($me):
    ?>
    <a href="<?php
        echo $logoutUrl;
    ?>"> <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif"> </a>
    <?php
    else:
    ?>
    <div> Using JavaScript &amp; XFBML:
          <fb:login-button></fb:login-button>
        </div>
    <?php
    endif;
    ?>
    <h3>Session</h3>
    <?php
        if($me):
    ?>
    <pre><?php
            print_r($session);
    ?></pre>
    <h3>You</h3>
    <img src="https://graph.facebook.com/<?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;
    ?>
    <?php
            print_r($me);
    ?>
    </body>
    </html>

When i click Login. It opens the window and when i allow permission. It refreshes the window. only to show the same content. when i do a print_r($me) nothing is visible.

When i check in the Application settings. The application has been installed in my fb account.

EDIT

enter image description here

enter image description here

Harsha M V
  • 54,075
  • 125
  • 354
  • 529
  • 7
    **NEVER EVER** share your application secret with **ANYONE**!! I suggest you re-generate a new code immediately! – ifaour Mar 15 '11 at 22:56
  • @ifaour i have changed a few char :) Thanks – Harsha M V Mar 16 '11 at 03:13
  • I see. Have a look at this [article](http://www.masteringapi.com/tutorials/how-to-develop-your-facebook-application-locally/6/) and [this](http://stackoverflow.com/questions/5133075/running-facebook-application-on-localhost) answer. – ifaour Mar 16 '11 at 08:00
  • @ifaour i followed that. when i click the Login Button it says "FB.login() called when user is already connected." in the console :( – Harsha M V Mar 16 '11 at 08:30
  • One VERY important note is remove the site domain, obviously it's wrong. Also have a look at [this bug](https://github.com/facebook/php-sdk/issues/7), it *might be* related. – ifaour Mar 16 '11 at 12:57

2 Answers2

2

Check that the Canvas URL you have set up into your app configuration is the same that the one which you are executing the code.

If it's not the same, this is a common behaviour.

Manuel Pedrera
  • 5,347
  • 2
  • 30
  • 47
  • i have added the screenshots.. its the same. i have redirected localhost.com to localhost in hosts file of the windows.. – Harsha M V Mar 16 '11 at 03:18
  • i get "FB.login() called when user is already connected." in the console – Harsha M V Mar 16 '11 at 08:30
  • 1
    To be honest, I have never tried with localhost as Canvas URL, I always work with an online server. Have you tried with this option? – Manuel Pedrera Mar 16 '11 at 09:31
  • since we are developing over SVN with a team. desperate to set it up locally :( – Harsha M V Mar 16 '11 at 11:17
  • I would try to make an upload only to see if that's the issue. Anyway, there's software that lets you automatically upload whenever you make a commit (phpStorm, for example) to the SVN server – Manuel Pedrera Mar 16 '11 at 12:47
1

i think the canvas url is not configured well as per your application . you have to configure it,otherwise it is the common phenomenan.

love
  • 132
  • 1
  • 2