我有一个 PHP 文件,它通过curl 调用另一个PHP 文件。我试图让第二个文件向第一个文件发送响应,让它知道它已启动。问题是第一个不能等待第一个完成执行,因为这可能需要一分钟或更长时间,我需要它立即发送响应,然后继续处理常规业务。我尝试在第二个文件的顶部使用回显,但第一个文件没有得到响应。
如何在未完成执行的情况下发回响应?
文件1.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;
?>
文件2.php
<?php
/* This is the top of the file. */
echo 'I started.';
.
.
.
// Other CODE
.
.
.
?>
当我运行 file1.php 时,结果是:“响应:”,但我希望它是“响应:我开始了”。我知道 file2.php 启动是因为“其他代码”被执行,但是 echo 没有发送回 file1.php,为什么?