How to make php script run another php script

前端 未结 9 1338
小蘑菇
小蘑菇 2021-02-07 15:45

I\'m trying to do exactly the same thing as in my previous Python question, but in PHP. See my previous (answered) question

PHP script from previous question does someth

相关标签:
9条回答
  • 2021-02-07 16:01

    I'm having trouble getting my previous answer to work, though I suspect it may be the fault of my own server, or perhaps new browsers refusing the close the connection when instructed (I'm really not a pro on how that shtuff works).

    If that method doesn't work for you, either, try this article on pseudo-multi-threading in PHP, and see if you have better luck :)

    0 讨论(0)
  • 2021-02-07 16:01

    This is a bit of unix/linux-only hack that might not work on shared web servers:

    file1.php

    <?php
    $somearg = escapeshellarg('blah');
    exec("php file2.php $somearg > /dev/null &");   
    

    file2.php

    <?php
    //do some stuff that will take a while
    //$argv should contain 'blah', and also it seems the name of the php file
    //this script will continue to run. You might want to set max_execution_time to a sensible value.
    
    0 讨论(0)
  • 2021-02-07 16:04

    If your remote script is accessible as simple webpage via http, you might try variant with http get request where response timeout set to minimal time for example via CURL:

    if( $curl = curl_init() ) {
      curl_setopt($curl, CURLOPT_URL, 'http://your_site/your_script.php');
      curl_setopt($curl, CURLOPT_TIMEOUT, 1);
      curl_exec($curl);      
      curl_close($curl);
    }
    

    but so you will have delay in 1 second in the your first script execution.

    0 讨论(0)
  • 2021-02-07 16:06

    If a user will be using a web browser to reach the php script, I would use ajax to call the second page.

    The user won't even know it is being called.

    See w3schools for a tutorial on AJAX

    0 讨论(0)
  • 2021-02-07 16:14

    If you need this other script to be run, depending on the client would not be wise.

    What I'd do is use the technique described in the answer to this StackOverflow question (which points to this comment in the PHP documentation) and include the other script as your post-processing.

    However, that comment was written in 2006, and things may have changed since then. Please give the technique a try (as I will be doing, just for fun) and see if it works for you :)

    0 讨论(0)
  • 2021-02-07 16:15

    You should look into pcntl_fork if you want a multithreaded application. Also look at ignore_user_abort.

    0 讨论(0)
提交回复
热议问题