Run PHP Task Asynchronously

后端 未结 15 1235
青春惊慌失措
青春惊慌失措 2020-11-22 15:15

I work on a somewhat large web application, and the backend is mostly in PHP. There are several places in the code where I need to complete some task, but I don\'t want to m

15条回答
  •  一整个雨季
    2020-11-22 15:40

    When you just want to execute one or several HTTP requests without having to wait for the response, there is a simple PHP solution, as well.

    In the calling script:

    $socketcon = fsockopen($host, 80, $errno, $errstr, 10);
    if($socketcon) {   
       $socketdata = "GET $remote_house/script.php?parameters=... HTTP 1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n";      
       fwrite($socketcon, $socketdata); 
       fclose($socketcon);
    }
    // repeat this with different parameters as often as you like
    

    On the called script.php, you can invoke these PHP functions in the first lines:

    ignore_user_abort(true);
    set_time_limit(0);
    

    This causes the script to continue running without time limit when the HTTP connection is closed.

提交回复
热议问题