0

I'm trying to figure out how to run long php scripts without making the user wait forever. I have a script that needs to run several functions that each take several seconds to run.

I'm wondering is there a way I can have my php script return its data to the caller before it has finished running?

 <?php
  echo http_response_code(200);
  sleep(60);
  run_other_code();
 ?>

A very simple example, but I'm looking for the script to echo a response then run the rest of the code in the background. Is something like that even possible?

Austin
  • 1,619
  • 7
  • 25
  • 51
  • 3
    You might look into a queue system of some form. – castis Jul 09 '18 at 20:30
  • You can have your initial ajax request start another php script on the command line in the background. It can then return immediately and let the user know it's "running". If that background script wrote it's status to a log file somewhere, subsequent ajax requests could check this log to see if/when the script is finished and return any result. – Jonathan Jul 09 '18 at 20:53
  • Pure PHP solution would be like explained here: https://stackoverflow.com/questions/2585656/threads-in-php – koalaok Jul 09 '18 at 21:32

1 Answers1

1

Use ajax calls for doing such tasks, have a look at the example below

$.ajax({
    url: "getData.php",
    error: function(){
        // this code will fire when timeout is reached
    },
    success: function(){
        //this code will fire when the script is finished doing the task
    },
    timeout: 61000 // always kepp it more than the time total time taken by your script
});

Hope it helps

Qirel
  • 25,449
  • 7
  • 45
  • 62
Harkal
  • 1,770
  • 12
  • 28
  • @Ice76 but thats a solution brother why dont you suggest somethin when pessimism takes over this happens to people – Harkal Jul 09 '18 at 20:41
  • @Ice76 The answer itself is not PHP, but it does _run_ PHP, and is a very good way to run PHP in the background, and would very likely work in this case for OP. – GrumpyCrouton Jul 09 '18 at 20:42
  • but you are assuming that a client is coming from a website, running his script, then want him to send ajax requests to more endpoints he probably doesnt have? – Ice76 Jul 09 '18 at 20:45
  • @HarKal im not pessimistic. but good answer to my comment? Why not run async PHP because he is already running the script? Or have PHP send requests using curl instead of it being client side and exposing server end points? – Ice76 Jul 09 '18 at 20:47
  • That would kind of work but I am already using ajax to make the initial call. I'm looking for a way for the php scrip to return then finish running the long script. That way the user doesn't have to wait for the ajax to finish as well – Austin Jul 09 '18 at 20:47
  • @Ice76 if u would have written all this insted of "this is not php" must not have made me what i did. u r genius. behave like one btw client will only get to know about the url of a file and on that file u can make a check if the request is coming from your server only else it will record the IP/ so not a big deal even if u use ajax – Harkal Jul 09 '18 at 20:50