Domanda

I am using rolling curl to get data from 40 other websites. As soon as the results are available for a website, they are being sent out immediately by using chunks.

To implement that , I added following headers :-

header("Transfer-encoding: chunked");
flush();

I also used a function to print the chunks :-

function print_chunks($chunk){
    $chunk = json_encode($tempArray);
    echo sprintf("%x\r\n", strlen(($chunk)));
    print_r($chunk);
   echo "\r\n";
   flush();
}

In my case , every chunk is some data in JSON format, whose size can be any value other than zero.

At the client side, I am using this to handle my responses :-

xml.onprogress = function () {
alert("Triggered");
}

That triggers only twice for about 40 calls . I guess many responses are getting merged before they are actually sent out. This leads to severely bad performance as results are sent out not individually, but only after all results have been sent out.Is it due to small size of individual responses ?

Here is the actual handle which sends chunked data to check out.

Update :

Is there any constraint on the minimum size of individual chunks ? If I sent only simple small string chunks, it sends all of my chunks together.

This is the complete code that i used. Even if I have made 10 chunks here, I get all of them together after 20 seconds :-

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

?>

Please comment if I am unclear in asking my doubt.

È stato utile?

Soluzione

Using flush() to push the content to the browser, at the end of your print_chunksfunction, should work.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top