Cory
Cory

Reputation: 61

How to disable PHP's exec function for printing to the shell?

I have the following php code:

exec('curl -X POST http://mysite.com', $output =array());

The return string my http://mysite.com is not displayed on the shell, but the following string is displayed:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     3    0     3    0     0     19      0 --:--:-- --:--:-- --:--:--     0

I don't want anything to display on the shell. How to I disable priting to the shell when using exec() command. There are other commands?

Upvotes: 4

Views: 3719

Answers (2)

Alfro
Alfro

Reputation: 517

The stdout stream output is captured by the php exec() function. However, the stderr stream is not, thus the reason why some things appear on screen and others don't.

In this case this happens becausecurl is outputting status info on the stderr stream.

Solutions:

  • Redirect stderr to the null device appending 2>/dev/null to the command executed by exec() (2> nul in Windows). This will dispose of any messages sent to stderr.

  • Or redirect the stderr stream to stdout by appending 2>&1 instead. This will allow capturing both outputs with exec() in case you need them in your php script.

For the particular case in the question, though, using curl's silent option may make more sense. :P

Upvotes: 7

KeatsKelleher
KeatsKelleher

Reputation: 10191

Use curl's silent option: -s

exec('curl -s http://us.php.net/manual/en/function.ob-clean.php', $output);

Upvotes: 8

Related Questions