【发布时间】:2013-01-09 21:56:39
【问题描述】:
我正在使用滚动 curl 从其他 40 个网站获取数据。一旦结果可用于网站,就会立即使用块发送出去。
为了实现这一点,我添加了以下标题:-
header("Transfer-encoding: chunked");
flush();
我还使用了一个函数来打印块:-
function print_chunks($chunk){
$chunk = json_encode($tempArray);
echo sprintf("%x\r\n", strlen(($chunk)));
print_r($chunk);
echo "\r\n";
flush();
}
在我的例子中,每个块都是一些 JSON 格式的数据,其大小可以是除零以外的任何值。
在客户端,我使用它来处理我的响应:-
xml.onprogress = function () {
alert("Triggered");
}
对于大约 40 次调用仅触发两次。我想许多回复在实际发出之前就被合并了。这会导致性能严重下降,因为结果不是单独发送,而是在所有结果发送后才发送。是因为单个响应的大小太小吗?
Here 是发送分块数据进行签出的实际句柄。
更新:
对单个块的最小大小有任何限制吗?如果我只发送简单的小字符串块,它会将我所有的块一起发送。
这是我使用的完整代码。即使我在这里制作了 10 块,我也会在 20 秒后将它们全部放在一起:-
<?php
header("Transfer-encoding: chunked");
flush();
function dump_chunk($chunk)
{
echo sprintf("%x\r\n", strlen($chunk));
echo $chunk;
echo "\r\n";
flush();
}
$string = "Hello World, This is chunk1";
$string1 = "Hello World, This is chunk2";
$string2 = "Hello World, This is chunk3";
$string3 = "Hello World, This is chunk4";
$string4 = "Hello World, This is chunk5";
$string5 = "Hello World, This is chunk6";
$string6 = "Hello World, This is chunk7";
$string7 = "Hello World, This is chunk8";
$string8 = "Hello World, This is chunk9";
$string9 = "Hello World, This is chunk10";
$string10 = "";
dump_chunk($string);
sleep(2);
dump_chunk($string1);
sleep(2);
dump_chunk($string2);
sleep(2);
dump_chunk($string3);
sleep(2);
dump_chunk($string4);
sleep(2);
dump_chunk($string5);
sleep(2);
dump_chunk($string6);
sleep(2);
dump_chunk($string7);
sleep(2);
dump_chunk($string8);
sleep(2);
dump_chunk($string9);
sleep(2);
dump_chunk($string10);
?>
如果我的疑问不清楚,请发表评论。
【问题讨论】:
-
您是否使用了任何干扰缓冲的 Apache 模块,例如
mod_gzip? -
不,我没有使用任何其他模块
-
有没有在线调试器可以提交我的句柄?
标签: php curl chunked-encoding