Viewed   425 times

How can I run PHP code asynchronously without waiting? I have a long run (almost infinite) that should run while server starts and should process asynchronously without waiting.

The possible options I guess are:

  1. Running the code in a web page and keep it open to do that task
  2. Calling the script from some command line utility (I am not sure how) which would process in the background.

I am running the PHP scripts on my local server which will send emails when certain events occur, e.g. birthday reminders.

Please suggest how can I achieve this without opening the page in a browser.



If you wanted to run it from the browser (perhaps you're not familiar with the command line) you could still do it. I researched many solutions for this a few months ago and the most reliable and simplest to implement was the following from How to post an asynchronous HTTP request in PHP


$params['my_param'] = $a_value;
post_async('http:://localhost/batch/myjob.php', $params);

 * Executes a PHP page asynchronously so the current page does not have to wait for it to     finish running.
function post_async($url, array $params)
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);  
    $post_string = implode('&', $post_params);


    $fp = fsockopen($parts['host'],
        $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1rn";
    $out.= "Host: ".$parts['host']."rn";
    $out.= "Content-Type: application/x-www-form-urlencodedrn";
    $out.= "Content-Length: ".strlen($post_string)."rn";
    $out.= "Connection: Closernrn";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);

Let's say the file above is in your web root directory (/var/www) for example and is called runjobs.php. By visiting http://localhost/runjobs.php your myjob.php file would start to run. You'd probably want to add some output to the browser to let you know it was submitted successfully and it wouldn't hurt to add some security if your web server is open to the rest of the world. One nice thing about this solution if you add some security is that you can start the job anywhere you can find a browser.

Tuesday, November 22, 2022

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (

  -r, --register-only
        do not remove files, only register the packages as not installed

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022

Under Windows it seems that files in vendor/bin are actually batch files, invoking the original file (and not php files which phpdbg will understand).

In this case:

dir=$(d=${0%[/\]*}; cd "$d"; cd "../phpunit/phpunit" && pwd)
"${dir}/phpunit" "$@"

i.e. ../phpunit/phpunit/phpunit (this path is relative to vendor/bin); thus the actual file is at vendor/phpunit/phpunit/phpunit.

And you can invoke it directly via phpdbg -qrr vendor/phpunit/phpunit/phpunit then.

Friday, December 23, 2022

@yanachen, this is now possible in VS Code. All you need to do is:
1. Ensure python is running in the VS Code terminal window
2. Select the text you wish to execute in python
3. Invoke the command 'workbench.action.terminal.runSelectedText' as defined in the following link:

Monday, October 17, 2022

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :