0

I have these lines in PHP:

public function run(){
    $command = 'php -S localhost:1337';
    exec($command);
}

I created a simple commands in php, something like this:

php file run

When the user enters this command, PHP goes to the method run() but the output message is empty and the server already are running.

But when I run the command php -S localhost:8000 directly in cli it shows this message:

C:\xampp\htdocs\hola>php -S localhost:8000
PHP 5.6.31 Development Server started at Mon Jan 15 15:52:34 2018
Listening on http://localhost:8000

Why the output message of the command line is not showing only when the command is executed from a file?.

cteski
  • 487
  • 3
  • 12
  • 17
Ivo Facundo
  • 105
  • 1
  • 8

3 Answers3

0

The exec function's signature is:

string exec ( string $command [, array &$output [, int &$return_var ]] )

So if you do something like the following, it should work:

public function run(){
    $output = array();
    $command = 'php -S localhost:1337';
    exec($command, $output);
    echo implode("\n", $output);
}

After the exec function completes, the output will be in $output with each output line as an element of the array. The implode function will then combine the elements of the array into a string separated by a newline, and then the echo statement will print this string.

pgngp
  • 1,552
  • 5
  • 16
  • 26
  • it still does not work, i have another methods, that the user can call from cli, but run() is the only one that doesnt work, it's like I execute the exec, and it would stop there, because I do an echo commenting all the lines and there's a text output. – Ivo Facundo Jan 15 '18 at 19:41
  • So you tried something like `echo implode("\n", $output);` after the `exec` line and it didn't show you any output? – pgngp Jan 15 '18 at 19:51
0

No one has mentioned the shell_exec function yet. Combine @pgngp and @DrKey's answers for this:

public function run(){
    $command = 'php -S localhost:1337';
    echo shell_exec($command);
}

The difference here is that exec only returns the last line of output where shell_exec returns the entire result of the command executed. Additionally, the backtic (`ls -al`) operator is the same as running shell_exec.

public function run(){
    echo `php -S localhost:1337`;
}
Ben Harris
  • 547
  • 3
  • 10
  • doesnt works too :C why? if i add an echo at the first line of the method something like this echo "hello". in the cli only shows "hello" and it stops there, i mean the command is succesfully executed but the output doesnt shows – Ivo Facundo Jan 15 '18 at 19:47
  • So the server does start? Are you just trying to use this as a shortcut for starting the development server? I'm not able to call a function defined in the php file from the command line. I'd recommend adding a `#!/usr/bin/env php` shebang to the top of the file, `chmod +x` and moving ``echo `php -S localhost:1337`;`` outside a method. You're not doing anything to parse `$argv` (which will be defined when calling from the command line). – Ben Harris Jan 15 '18 at 19:55
  • Try calling `run();` at the end of the file. – Ben Harris Jan 15 '18 at 19:57
0

exec doesn't returns output in realtime, just at command execution end, but php -S is never ends. I suppose you are looking for something like proc_open. Probably this answer helps you

akaincore
  • 331
  • 1
  • 9