3

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.

Mshah
  • 61
  • 6
  • 1
    Save yourself all this effort, try using https://blueimp.github.io/jQuery-File-Upload/ – RiggsFolly Sep 16 '15 at 10:06
  • Thanks, but I've already made all the effort. The file upload, handling, php processing, retrieving status updates from external program, etc have all been done. I just need to figure out how to output json responses in real-time (or every 2/3 seconds). Thanks – Mshah Sep 16 '15 at 10:42
  • Even still, the whole thing exists. Don't be afraid to drop what you've done for something that works, that has multiple contributors and given the amount of work it's had done on it, will likely be more stable, reliable and secure. – Popnoodles Sep 16 '15 at 11:32
  • Perhaps will check out for v2.0. For now I went down the txt file route. So on the default page, I have PHP generate a unique text file name, apply that to a hidden input form field, then pass that through post into the submit page. There I create this text file and use file_put_contents (ensures I only have one line of text to read) to save the status message in the text file. So back in the default page, I had Jquery perform a get function every 2 seconds on the unique text file. Once Jquery gets the success response, I stopped the get function interval and deleted the text file. – Mshah Sep 16 '15 at 22:20
  • Maybe you can look into this http://stackoverflow.com/questions/25557075/passing-url-params-with-jquery-into-php/25557666#25557666 – kay Oct 19 '15 at 07:38
  • Maybe use XMLHttpRequest should be better, see [1]: http://stackoverflow.com/questions/17931775/can-i-get-realtime-php-data-to-appear-in-a-jquery-dialog-box – phanx Nov 07 '15 at 04:22

0 Answers0