PHP Curl 输出缓冲区未收到响应
我有一个协议,其中 file1.phpcurl 运行 file2.php。 file2.php 是一个长时间运行的文件,但它发送(或应该发送)一个响应回 file1.php,然后继续执行它的代码。我正在使用输出缓冲区来尝试发送此数据,但问题是如果我“返回;”冲洗后立即; file1.php 接收响应很好,但是当我尝试保持 file2.php 运行时,file1.php 永远不会接收响应,我做错了什么?有没有其他方法可以将响应发送回 file1.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.
I have a deal where file1.php curl runs file2.php. file2.php is a long running file, but it sends(or is supposed to send) a response back to file1.php then carry on with it's code. I am using output buffer to try sending this data, but the problem is if I 'return;' right after the flush; file1.php receives the response just fine, but when I try to keep file2.php running, file1.php never receives the response, what I am doing wrong? Is there a different way I must send the response back to file1.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在正常的卷曲传输中,在页面完成加载之前,您将无法获取数据。你的脚本已经完成了。如果您想使用部分数据,您应该查看 CURLOPT_WRITEFUNCTION 。这将创建一个回调,只要有任何数据可用,您就可以使用该回调。
In a normal curl transfer you wouldn't be able to get the data until the page has completed loading ie. your script is finished. If you want to work with partial data, you should look at CURLOPT_WRITEFUNCTION . This creates a callback which you can use whenever any data is available.