Facebook Connect 获取用户数据

发布于 2024-10-21 20:43:49 字数 3612 浏览 2 评论 0原文

我遵循 Facebook PHP SDK 示例并创建了以下内容。

<?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>

当我点击登录时。当我允许时它会打开窗口。它刷新了窗口。只是为了显示相同的内容。当我执行 print_r($me) 时,什么都看不见。

当我检查应用程序设置时。该应用程序已安装在我的 Facebook 帐户中。

编辑

在此处输入图像描述

在此处输入图像描述

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 & 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁客 2024-10-28 20:43:49

检查您在应用程序配置中设置的 Canvas URL 是否与您正在执行代码的 URL 相同。

如果不一样,这是一种常见行为。

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.

贩梦商人 2024-10-28 20:43:49

我认为画布 URL 的配置不符合您的应用程序。
你必须配置它,否则这是常见现象。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文