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