facebook sdk php example not working

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

I am trying to develop facebook login for a website, i tried the example given with php-facebook-sdk even after login to facebook the $user variable is still 0 even after facebook login it is not showing logout url.

call to $facebook->getUser(); function is allways returning 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> 

回答1:

Facebook user can return 0 if there is no user logged in. The case that you do have a user means that you have a user logged in to facebook via your application. That also does not mean that you have permissions to view the user's details. Try this simple example I wrote some time ago but still works.

Now take care this is a redirect controller. As you can see it only checks and asks for user's. Use it as an 'intermediate' controller and base your templating HTML logic with the JSFBSDK or every time you need a user to take an action redirect him here (eg login etc)

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!