How to call another PHP script from a PHP script?

后端 未结 3 1445
情歌与酒
情歌与酒 2021-01-27 19:30

I have a PHP script that has a runtime of 34 seconds. But it dies after 30 seconds. I guess my webhost a time limit of 30 seconds.

I am thinking of splitting the script

3条回答
  •  余生分开走
    2021-01-27 20:05

    You should use the set_time_limit() function, it helps in most cases.

    Alternatively, on Linux/Unix, you can try running the script as a background process. PHP CLI can be used for this purpose, the scripts running via CLI have no time limit. You can use exec/system or similar PHP functions to fireup the PHP CLI and have it run a PHP script in background, returning control to the script immediately. In most cases, a PHP script running via CLI behaves just like it would do in CGI environment except for few environment related differences, such as no time limit.

    Here is one way to do it:

    exec("/usr/bin/php script.php > /dev/null &");
          ^            ^          ^           ^
          |            |          |           |
          |            |          |           +-- run the specified process in background
          |            |          +-------------- redirect script output to nothing
          |            +------------------------- your time consuming script
          +-------------------------------------- path to PHP CLI (not PHP CGI)
    

    More details at: Running a Background Process in PHP

提交回复
热议问题