我有一个发出一堆cURL请求的PHP脚本。在每次cURL请求之后,我想回显一些数据,但目前,数据仅在每5-10个cURL请求后回显。
我尝试过使用ob_flush和flush,但似乎没有什么不同。以下是我的脚本的基本流程:
<?php
header('Content-Type: text/html; charset=UTF-8');
set_time_limit(0);
ob_start();
$arr = array(); // Lots of strings in this array
foreach ($arr as $elem) {
// Use $elem to make cURL request and return HTML.
// Run regexes on returned HTML.
echo '<pre>';
print_r($matches[1]);
print_r($matches[2]);
echo '</pre>';
ob_flush();
flush();
}我可以做些什么来强制脚本在foreach循环的每次迭代后输出回显/print_r数据?
非常感谢。
发布于 2013-12-02 03:47:13
您需要在循环中移动ob_start(),如下所示:
<?php
header('Content-Type: text/html; charset=UTF-8');
set_time_limit(0);
$arr = array(); // Lots of strings in this array
foreach ($arr as $elem) {
ob_start();
// Use $elem to make cURL request and return HTML.
// Run regexes on returned HTML.
echo '<pre>';
print_r($matches[1]);
print_r($matches[2]);
echo '</pre>';
ob_end_flush();
flush();
}可以将输出缓冲区(ob_*)函数看作堆栈上的push和pop。您可以通过将缓冲区压入堆栈(ob_start())来指定开始记录的位置,然后当您想要输出时,将缓冲区弹出堆栈并对结果执行某些操作(ob_flush()、ob_get_*()等)。每个ob_start()必须有一个匹配的缓冲区结束函数。
您还会希望使用ob_end_flush()而不是ob_flush(),因为我认为您不希望在每次运行后都保留缓冲区。
发布于 2013-12-02 04:21:10
在开始时尝试使用此命令:
apache_setenv('no-gzip', 1);
ini_set('output_buffering', 0);
ini_set('zlib.output_compression', 0);
ini_set('implicit_flush', 1);然后做你已经做过的事情。
https://stackoverflow.com/questions/20316338
复制相似问题