如何在后台运行 fql 请求? (通过php异步调用api)

发布于 2024-12-02 17:53:48 字数 1214 浏览 1 评论 0原文

我想在后台运行一个特别昂贵的 fql 查询,将结果记录到数据库中,并稍后检索它,而无需用户等待每个步骤。

您能分享一个如何异步运行 Facebook 请求的示例吗?

ma​​in.php

$uid = $facebook->getUser();
if ($uid) {
    try {
        echo $user;

        ////////////////////////////////////////////////////////
        // Run lengthy query here, asynchronously (async.php) //
        ////////////////////////////////////////////////////////
        //                                                    //
        // For example: $profile = $facebook->api('/me');     //
        // (I know this request doesn't take long, but        //
        // if I can run it in the background, it'll do.       //
        //                                                    //
        ////////////////////////////////////////////////////////


    } catch (FacebookApiException $e) {
        echo $e;
    }
}

async.php

$profile = $facebook->api('/me');
$run = mysql_query("INSERT INTO table (id) VALUES (" . $profile['id'] . ");";

complete.php

echo getProfileId(); // assume function grabs id from db, as stored via async.php

I'd like to run a particularly expensive fql query in the background, log results to the database, and retrieve it later without the user having to wait for each step.

Can you share an example of how to run a facebook request asynchronously?

main.php

$uid = $facebook->getUser();
if ($uid) {
    try {
        echo $user;

        ////////////////////////////////////////////////////////
        // Run lengthy query here, asynchronously (async.php) //
        ////////////////////////////////////////////////////////
        //                                                    //
        // For example: $profile = $facebook->api('/me');     //
        // (I know this request doesn't take long, but        //
        // if I can run it in the background, it'll do.       //
        //                                                    //
        ////////////////////////////////////////////////////////


    } catch (FacebookApiException $e) {
        echo $e;
    }
}

async.php

$profile = $facebook->api('/me');
$run = mysql_query("INSERT INTO table (id) VALUES (" . $profile['id'] . ");";

complete.php

echo getProfileId(); // assume function grabs id from db, as stored via async.php

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

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

发布评论

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

评论(1

洛阳烟雨空心柳 2024-12-09 17:53:48

我在后台运行 PHP 作业的解决方案只是让脚本本身发出一个执行实际作业的新请求。

我之前使用过的代码如下,我不确定是否有更优雅的解决方案......但它过去对我来说已经足够好了。我使用 fsock 而不是 file_get_contents() 等的原因当然是这样我就不必等待作业完成(这会达不到目的)

$sock = fsockopen("127.0.0.1", 80);
fwrite($sock, "GET /yourjoburl/ HTTP/1.1\r\n");
fwrite($sock, "Host: yourdomain.com\r\n");
fwrite($sock, "Connection: close\r\n");
fwrite($sock, "\r\n");
fflush($sock);
fclose($sock);

所以,然后你只需让其他脚本将结果和进度写入数据库或其他什么...还要记住MySQL支持互斥体,这意味着您可以轻松地防止多个作业同时运行...或者允许其他脚本等待作业完成。

附言。我个人避免 exec 和所有这些东西的原因是,它似乎使用起来很麻烦,不同的服务器,不同的设置,不同的操作系统等。这在所有允许您打开套接字的主机上都是相同的。尽管您可能希望在作业中验证的请求中添加私钥,或者检查调用者的 IP,以防止其他人能够启动作业。

编辑:这未经测试,但如果您也想转发 cookie(包括会话 cookie),应该可以工作。

$sock = fsockopen("127.0.0.1", 80);
fwrite($sock, "GET /yourjoburl/ HTTP/1.1\r\n");
fwrite($sock, "Host: yourdomain.com\r\n");
fwrite($sock, "Cookie: " . $_SERVER['HTTP_COOKIE'] . "\r\n");
fwrite($sock, "Connection: close\r\n");
fwrite($sock, "\r\n");
fflush($sock);
fclose($sock);

My solution to running PHP jobs in the background is just to have the script itself make a new request which executes the actual job.

The code I've used before is the following, I'm not sure if there are more elegant solutions... but it has worked more than well enough for me in the past. The reason I use fsock and not file_get_contents() etc is of course so that I won't have to wait for the job to finish (which would defeat the purpose)

$sock = fsockopen("127.0.0.1", 80);
fwrite($sock, "GET /yourjoburl/ HTTP/1.1\r\n");
fwrite($sock, "Host: yourdomain.com\r\n");
fwrite($sock, "Connection: close\r\n");
fwrite($sock, "\r\n");
fflush($sock);
fclose($sock);

So, then you just have the other script write the results and progress to a database or whatever... also remember that MySQL supports mutexes, which means you can easily prevent multiple jobs from running at the same time... or to allow other scripts to wait for the job to finish.

PS. The reason I personally avoid exec and all that stuff is that it just seems like a hassle to work with, different servers, different setups, different OSes, etc. This works the same on all hosts that allow you to open sockets. Although you might want to add a private key to the request that you verify in the job, or check the IP of the caller, to prevent others from being able to start jobs.

EDIT: This is untested but should work if you want to forward the cookies as well, including the session cookie.

$sock = fsockopen("127.0.0.1", 80);
fwrite($sock, "GET /yourjoburl/ HTTP/1.1\r\n");
fwrite($sock, "Host: yourdomain.com\r\n");
fwrite($sock, "Cookie: " . $_SERVER['HTTP_COOKIE'] . "\r\n");
fwrite($sock, "Connection: close\r\n");
fwrite($sock, "\r\n");
fflush($sock);
fclose($sock);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文