简体   繁体   中英

Facebook $facebook->getUser() very very slow

I've got a problem with the facebook PHP-SDK in my oauth2 app. I use login via the JS-SDK but handle most of the app via PHP.

Here is my PHP code i use to handle this:

<?php
    $uid            =   null; 

    include_once "lib/facebook.php";

    if (stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
    {
        header('p3p: CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"');
    }

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

    $user = $facebook->getUser(); // this take for ever

        if ($user) {    
            try {
                $uid      =   trim($user);
                $me     =   $facebook->api('/me');

            } catch (FacebookApiException $e) {   
              echo $e;
                try {
                    $uid      =   trim($user);
                    $me     =   $facebook->api('/me');

                } catch (FacebookApiException $e) {  

                }
            }
        }
?>

I found out that $facebook->getUser(); is slowing down my app in the last 3 hours, some times it take up to 2 minutes until the script continues. But must of the time its 30+ seconds. Is there a way to cache this or a way to speed this up again?

You can also speed up your site by avoiding

$user = $facebook->getUser();

whenever you can. I had a performance problem in the order of half a second for each and every page on my site, because I was calling that function to check if my user is still logged in or not.

This check might be essential for you, but in my case, it is not. If it is OK for your site to sacrifice that check until the user hits the logout button, replacing

$user = $facebook->getUser();

with

if(! isset($_SESSION["fb_<my App ID>_user_id"])) // Replace <my App ID> with yours
  $user = $_SESSION["facebook"]->getUser();

and by calling

if(isset($_SESSION["fb_<my App ID>_user_id"]))
  unset($_SESSION["fb_<my App ID>_user_id"]);

on your logout page. This will speed up the entire site.

In case your user is not logged in, you will effectively call getUser() very often. But this seems to work very fast, if the user is not logged in.

问题得到解决,API再次反应得足够快。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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