ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

facebook sdk php示例无效

2019-07-08 22:30:00  阅读:165  来源: 互联网

标签:php facebook facebook-php-sdk


我正在尝试为一个网站开发facebook登录,我尝试了用php-facebook-sdk给出的例子,即使在登录到facebook之后,即使在Facebook登录后它还没有显示注销网址,$user变量仍为0.

拨打$facebook-> getUser();功能总是返回0.

  require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'xxx',
  'secret' => 'xxxx',
));

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

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

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;
  }
}
print_r($user);
// 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('/verity.vis');

?>
<!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 $statusUrl; ?>">Check the login status</a>
      </div>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $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/verity.vis/picture">
    <?php echo $naitik['name']; ?>
  </body>
</html>

解决方法:

如果没有用户登录,Facebook用户可以返回0.
您拥有用户的情况意味着您有一个用户通过您的应用程序登录到Facebook.
这也并不意味着您有权查看用户的详细信息.试试我前段时间写的这个简单例子,但仍然有用.

现在注意这是一个重定向控制器.如您所见,它只检查并询问用户.将它用作“中间”控制器,并使用JSFBSDK创建模板化HTML逻辑,或者每当您需要用户将操作重定向到此处时(例如登录等)

// 0. We don't need to clear session
require_once realpath(dirname(__FILE__)) . 'facebook.php';

$current_url = ''; // replace

$facebook = new Facebook(array(
    'appId' => '',
    'secret' => '',
));

$fbCurrentUserID = $facebook->getUser();

// 1. If we cant get the user log him in and request permissions This requests combined permissions (basic + post)
if (!$fbCurrentUserID){
    $loginUrl = $facebook->getLoginUrl(array(
        'scope' => 'email, publish_actions', // Put the permissions you like            
        'redirect_uri' => $current_url, // Replace here
    ));
    die(header('Location: ' . $loginUrl));
}

// 2. So we do have a current FB user. Lets try to read data (Maybe he declined perms)
try {

    $user_profile = $facebook->api('/me');

} catch (FacebookApiException $e) {
    // 2.b Log any error and retry please

    die(header('Location: /'));
}

//Continue your code execution here after all up above went well

标签:php,facebook,facebook-php-sdk
来源: https://codeday.me/bug/20190708/1406766.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有