简体   繁体   中英

PHP: execute command, then continue?

I'm writing a PHP-GTK Twitter client, and when I do OAuth authentication, I need to open a browser to show the user the out-of-band token. I also need to have a dialog box open and operational during this time for the user to enter in that token.

The problem is, on Linux, when I open the browser with xdg-open URL , PHP seems to pause until I close the browser window. I need PHP to launch the browser, then open the dialog. Can I make PHP just execute the command and continue on with the script?

On *nix, you can launch an external program and continue (ie not block) using the & operator. You also have to redirect STDOUT and STDERR to somewhere in order for this to work correctly.

So you would do something like this:

exec("xdg-open \"$url\" > /dev/null 2>&1 &");

There is also a way to do it on Windows, which I will add when I find it.

EDIT

You can achieve the same thing of Windows using the following snippet:

$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run($commandToExecute, 0, FALSE);

The FALSE argument passed in the second line is the part that stops it from blocking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM