简体   繁体   English

Facebook API PHP-SDK

[英]Facebook api php-sdk

I started playing around with the fb api a couple of days ago. 几天前,我开始使用fb api。 I am using the fb php-sdk to authenticate users with facebook on my site. 我正在使用fb php-sdk在我的网站上通过Facebook验证用户身份。 I use the following piece of code which works, but intermittently and I cant seem to figure out why. 我使用下面的代码可以正常工作,但是断断续续,我似乎无法弄清楚为什么。

    <?php
    session_start();


    require './library/facebook.php';
    include './library/fb_keys.php';

    // Create app instance
    $facebook = new Facebook(array(
   'appId'  => YOUR_APP_ID,
   'secret' => YOUR_APP_SECRET,
    ));

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

   //Check Access token

   if ($user) {
   try {
   // Proceed with logged in user.
   $user_profile = $facebook->api('/me');
   } catch (FacebookApiException $e) {
   //error_log($e);
   $user = null;
    }
   }

 // Login or logout based on user state
 if ($user) {
 $logoutUrl = $facebook->getLogoutUrl();

   } else {
  $loginUrl = $facebook->getLoginUrl(array('scope' => 'status_update, publish_stream', 'redirect_uri' => 'http://webaddress.com/web/'));

    }

    ?>

This is really straightforward piece of code that I got from the https://github.com/facebook/php-sdk/ . 这是我从https://github.com/facebook/php-sdk/获得的非常简单的代码。

I usually can't log in when I clear all my cookies and/or session is cleared. 当我清除所有cookie和/或会话被清除时,通常无法登录。 but once I log on to the facebook.com site all seems to work again. 但是,一旦我登录到facebook.com网站,一切似乎都可以再次使用。 So it indicates that I am missing something with regard to setting the session or cookies. 因此,这表明我在设置会话或cookie方面缺少任何东西。

Any help will be appreciated. 任何帮助将不胜感激。

I guess by not working you mean that user isn't redirected to the login page? 我想通过不工作,您是指用户未重定向到登录页面吗?

You do get the loginUrl but you're not redirecting user there. 您确实获得了loginUrl,但您没有在那里重定向用户。

This can be easly done with adding the following code after $loginUrl line: 只需在$ loginUrl行之后添加以下代码,即可轻松完成此操作:

print '<script language="javascript" type="text/javascript"> top.location.href="'. $loginUrl .'"; </script>';

If I'm wrong please tell me exactly what you mean under "can't log in", what stops you etc. 如果我错了,请在“无法登录”下准确告诉我您的意思,阻止您的事情等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM