我怎样才能在后台运行的FQL请求? (异步API通过PHP调用) [英] How can I run an fql request in the background? (asynchronous api calls via php)

查看:123
本文介绍了我怎样才能在后台运行的FQL请求? (异步API通过PHP调用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在后台运行一个特别昂贵FQL查询,结果记录到数据库中,并在以后检索,而不必等待每个步骤的用户。

您可以分享如何异步运行一个Facebook要求的例子吗?

main.php

  $ UID = $ facebook->的getUser();
如果($ UID){
    尝试{
        回声$用户;        ////////////////////////////////////////////////// //////
        //运行冗长这里查询,异步(async.php)//
        ////////////////////////////////////////////////// //////
        // //
        //例如:$配置= $ facebook-> API('/我'); //
        //(我知道这个要求并不需要很长时间,但//
        //如果我可以在后台运行它,它会做。 //
        // //
        ////////////////////////////////////////////////// //////
    }赶上(FacebookApiException $ E){
        回声$ê;
    }
}

async.php

  $外形= $ facebook-> API('/我');
$ RUN =的mysql_query(INSERT INTO表(ID)VALUES($简介['身份证']。);

complete.php

 回声getProfileId(); //假设功能抓起ID从数据库,经由async.php存储


解决方案

我的解决方案,以在后台运行PHP的工作就是让脚本本身使后者执行实际工作的新要求。

在code我以前用过的是下面的,我不知道如果有更多的优雅的解决方案...但它在过去的工作不是不够好,更适合我。我用fsock而不是file_get_contents()函数等原因当然是这样我就不必等待作业完成(这会破坏目的)

  $袜子=的fsockopen(127.0.0.1,80);
FWRITE($袜子,GET / yourjoburl / HTTP / 1.1 \\ r \\ n);
FWRITE($袜子,主机:yourdomain.com \\ r \\ n);
FWRITE($袜子,连接:关闭\\ r \\ n);
FWRITE($袜子,\\ r \\ n);
fflush($袜子);
FCLOSE($袜子);

这样,那么你只是有其他脚本编写的成果和进展到一个数据库或任何...还记得,MySQL支持互斥体,这意味着你可以很容易地prevent多个作业从同时运行。 ..或允许其他脚本等待作业结束。

PS。我之所以亲自避免exec和所有的东西是,它似乎只是一个麻烦与不同服务器,不同的设置,不同的操作系统等,这对工作,让您打开插座的所有主机相同的工作。虽然你可能希望从能够启动作业添加一个私钥,您在工作中验证,或检查调用者的IP请求,以prevent他人。

编辑:这是未经测试,但如果你要转发的饼干,以及包括会话cookie应该工作

  $袜子=的fsockopen(127.0.0.1,80);
FWRITE($袜子,GET / yourjoburl / HTTP / 1.1 \\ r \\ n);
FWRITE($袜子,主机:yourdomain.com \\ r \\ n);
FWRITE($袜子,曲奇$ _ SERVER ['HTTP_COOKIE']为\\ r \\ n);
FWRITE($袜子,连接:关闭\\ r \\ n);
FWRITE($袜子,\\ r \\ n);
fflush($袜子);
FCLOSE($袜子);

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

解决方案

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);

这篇关于我怎样才能在后台运行的FQL请求? (异步API通过PHP调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆