如何在后台运行 fql 请求? (通过php异步调用api)
我想在后台运行一个特别昂贵的 fql 查询,将结果记录到数据库中,并稍后检索它,而无需用户等待每个步骤。
您能分享一个如何异步运行 Facebook 请求的示例吗?
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在后台运行 PHP 作业的解决方案只是让脚本本身发出一个执行实际作业的新请求。
我之前使用过的代码如下,我不确定是否有更优雅的解决方案......但它过去对我来说已经足够好了。我使用 fsock 而不是 file_get_contents() 等的原因当然是这样我就不必等待作业完成(这会达不到目的)
所以,然后你只需让其他脚本将结果和进度写入数据库或其他什么...还要记住MySQL支持互斥体,这意味着您可以轻松地防止多个作业同时运行...或者允许其他脚本等待作业完成。
附言。我个人避免 exec 和所有这些东西的原因是,它似乎使用起来很麻烦,不同的服务器,不同的设置,不同的操作系统等。这在所有允许您打开套接字的主机上都是相同的。尽管您可能希望在作业中验证的请求中添加私钥,或者检查调用者的 IP,以防止其他人能够启动作业。
编辑:这未经测试,但如果您也想转发 cookie(包括会话 cookie),应该可以工作。
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)
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.