Facebook $facebook->getUser() 非常非常慢

发布于 2024-12-09 17:39:07 字数 1107 浏览 1 评论 0原文

我的 oauth2 应用程序中的 facebook PHP-SDK 有问题。我通过 JS-SDK 登录,但通过 PHP 处理大部分应用程序。

这是我用来处理这个问题的 PHP 代码:

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

                }
            }
        }
?>

我发现 $facebook->getUser();在过去 3 小时内减慢了我的应用程序的速度,有时需要长达 2 分钟才能继续执行脚本。但肯定是 30 秒以上。有没有办法缓存这个或者再次加快速度?

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?

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

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

发布评论

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

评论(2

走走停停 2024-12-16 17:39:07

避免这样做,从而加快您的网站速度

$user = $facebook->getUser();

您还可以尽可能 。我的网站上的每个页面都遇到了大约半秒的性能问题,因为我正在调用该函数来检查我的用户是否仍然登录。

这项检查对您来说可能很重要,但就我而言,并非如此。如果您的网站可以牺牲该检查,直到用户点击注销按钮,

$user = $facebook->getUser();

来替换和

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

则可以通过调用

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

您的注销页面 。这将加快整个网站的速度。

如果您的用户未登录,您将经常有效地调用 getUser()。但如果用户未登录,这似乎工作得非常快。

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.

走野 2024-12-16 17:39:07

问题已解决,API 的反应又足够快了。

Problem is resolved, the API is reacting fast enough again.

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