0
我有一个交易,其中file1.php卷曲运行file2.php。 file2.php是一个长时间运行的文件,但它发送(或应该发送)回到file1.php的响应,然后继续执行它的代码。我使用输出缓冲区来尝试发送这些数据,但问题是如果我'返回';'冲洗后立即; file1.php收到响应就好了,但是当我试图保持file2.php运行时,file1.php永远不会收到响应,我做错了什么?有什么不同的方式,我必须将响应发送回file1.php?PHP卷曲输出缓冲区没有收到回应
// file1.php
$url = "file2.php"
$params = array('compurl'=>$compurl,'validatecode'=>$validatecode);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // 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_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);
curl_close($ch);
echo $response;
// file2.php
ob_start();
echo 'Running in the background.';
// get the size of the output
$size = ob_get_length();
header("HTTP/1.1 200 OK"); // I have tried without this
header("Date: " . date('D, j M Y G:i:s e')); // Tried without this
header("Server: Apache"); // Tried without this
header('Connection: close');
header('Content-Encoding: none');
header("Content-Length: $size");
header("Content-Type: text/html"); // Tried without this
// flush all output
ob_end_flush();
ob_flush();
flush();
// If I add return; here file1.php gets the response just fine
// But I need file2.php to keep processing stuff and if I remove the
// return; file1.php never gets a response.
但在另一个stackoverflow问题它表明,这是可能的:http://stackoverflow.com/questions/2996088/how-do-you-run-a-long-php-script-and-keep-sending-更新到浏览器通过http –
我试过CURLOPT_WRITEFUNCTION,但它仍然没有读任何响应..我如何循环,以便它等待这个部分响应,不退出脚本,但也没有'要占用资源? –
Curl不是浏览器。您的浏览器可以显示部分数据,但curl只会在页面完成后才会返回。坦率地说,我不能想象一个函数如何返回部分数据,除非你使用多线程。要使用部分数据,您需要一个CURLOPT_WRITEFUNCTION提供的回调。 – Ravi