2013-01-09 21 views
1

我正在使用滚动卷曲从其他40个网站获取数据。只要结果可用于网站,它们就会立即通过使用块发送出去。使用分块传输编码发送通过滚动获得的数据Curl

要实现这一点,我添加以下标题: -

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); 

?> 

如果我不清楚询问我的疑问,请发表评论。

+0

你是否在使用任何干扰缓冲的Apache模块,比如'mod_gzip'? – rmobis

+0

不,我没有使用任何其他模块 –

+0

有没有在线调试器,我可以提交我的句柄? –

回答

1

使用flush()将内容推送到浏览器,在print_chunks函数结束时,应该可以工作。

+0

我真的用过。我忘了在这里发布。你可以检查我提供的句柄,找出有多少块数据即将到来http://compare.buyhatke.com/example.php?searchText=Dumb%20and%20dumber –

+0

这只是给我一个空白页面。什么也没有,甚至在源代码上也没有。 – rmobis

+0

请等待约30秒。还要确保在末尾有一些searchText –