I need help with my issue of not being able to output JSON responses in real-time using JQUERY whilst PHP calls an external command line script.
Firstly, I'm using this JQUERY AJAX file upload with progress bar example to handle the file upload. So here you can pretty much see my coding for that mechanism. Although I added "dataType: 'json'" to the options to handle the response.
In the PHP script, I handle the file upload, clean up the file name, etc. Then I call an external cmd process. This process throws out updates such as "Completed: 10%", through to "Completed: 100%". This is the info that I wanna throw out to the browser via json. Here is my code for this:
ob_implicit_flush(true);
ob_end_flush();
$cmd = //call exe with arguements
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w")
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, NULL, NULL);
if (is_resource($process)) {
$return = array();
while ($s = fgets($pipes[1])) {
$return['status'] = substr($s,0,16);
echo json_encode($return);
//file_put_contents($file, json_encode($return));
flush();
}
}
proc_close($process);
This throws out this json output:
{"status":"Completed: 10%\r\n"}
{"status":"Completed: 13%\r\n"}
{"status":"Completed: 16%\r\n"}
... truncated ...
{"status":"Completed: 90%\r\n"}
{"status":"Completed: 95%\r\n"}
{"status":"Completed: 100%\r","output":"<p><img src=\"images\/accept.png\" width=\"16\" height=\"16\" alt=\"\" valign=\"top\" \/> Successfully converted:<br \/><br \/><a href=\"test.zip\">test.zip<\/a><\/p>"}
So, I want to be able to output the status values every 2/3 seconds. The final output value is outputted in the JQUERY success function.
Please bear in mind, I'm a lightweight user of JQUERY and AJAX.
[EDIT] The alternate route would be for me to write the status message(s) to a unique txt file and have JQUERY poll that every few seconds.