PHP Curl 异步响应
我有一个 PHP 文件,它通过curl 调用另一个PHP 文件。我试图让第二个文件向第一个文件发送响应,让它知道它已启动。问题是第一个不能等待第一个完成执行,因为这可能需要一分钟或更长时间,我需要它立即发送响应,然后继续处理常规业务。我尝试在第二个文件的顶部使用回显,但第一个文件没有得到响应。 如何在未完成执行的情况下发回响应?
file1.php
<?php
$url = 'file2.php';
$params = array('data'=>$data,'moredata'=>$moredata);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_TIMEOUT => 10, // don't wait too long
CURLOPT_POST => true, // Use Method POST (not GET)
CURLOPT_POSTFIELDS => http_build_query($params)
);
$ch = curl_init($url);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch); // See that the page started.
curl_close($ch);
echo 'Response: ' . $response;
?>
file2.php
<?php
/* This is the top of the file. */
echo 'I started.';
.
.
.
// Other CODE
.
.
.
?>
当我运行 file1.php 时,结果是:“响应:”,但我希望它是“响应:我开始了”。我知道 file2.php 启动是因为“其他代码”被执行,但是 echo 没有发送回 file1.php,为什么?
I have a PHP file that invokes another PHP file via curl. I am trying to have the second file send a response back to the first to let it know that it started. The problem is the first can't wait for the first to finish execution because that can take a minute or more, I need it to send a response immediately then go about it's regular business. I tried using an echo at the top of the second file, but the first doesn't get that as a response.
How do I send back a response without finishing execution?
file1.php
<?php
$url = 'file2.php';
$params = array('data'=>$data,'moredata'=>$moredata);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_TIMEOUT => 10, // don't wait too long
CURLOPT_POST => true, // Use Method POST (not GET)
CURLOPT_POSTFIELDS => http_build_query($params)
);
$ch = curl_init($url);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch); // See that the page started.
curl_close($ch);
echo 'Response: ' . $response;
?>
file2.php
<?php
/* This is the top of the file. */
echo 'I started.';
.
.
.
// Other CODE
.
.
.
?>
When I run file1.php it results in: 'Response: ' but I expect it to be 'Response: I started.' I know that file2.php gets started because 'Other CODE' get executed, but The echo doesn't get sent back to file1.php, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能正是您正在寻找的。 PHP 中的分叉:
http://framework.zend.com /manual/en/zendx.console.process.unix.overview.html
进程分为两部分。一个是另一个的父亲。父亲可以告诉客户他刚刚开始,孩子可以完成这项工作。当孩子完成后,他可以向父亲报告,父亲也可以向客户报告。
请记住,运行它有很多要求:
This could be just what you're looking for. Forking in PHP:
http://framework.zend.com/manual/en/zendx.console.process.unix.overview.html
A process divides in two. One is father of the other. The father can tell the client he just begun and the child can do the job. When the child finishes, he's able to report the father which can also report to the client.
Keep in mind there are many requirements for this to run:
答案最终是 CURL 的行为不像浏览器:
PHP Curl 输出缓冲区没有收到响应
我最终首先运行我的第二个文件,然后运行我的第一个文件。第二个文件等待第一个文件完成后的“完成”文件写入。
在这一点上,数据库似乎是一个更好的地方来存储消息,以便文件能够在彼此之间传递,但文件也可以用于快速而肮脏的工作。
The answer ended up being that CURL does not behave like a browser:
PHP Curl output buffer not receiving response
I ended up running my 2nd file first and my 1st file second. The 2nd file waited for a 'finished' file write that the 1st file did once it, obviously, finished.
At this point, it seems like the database would be a better place to store messages for files to be able to pass between each other, but a file would also work for a quick and dirty job.