在 PHP 中執行外部程式可使用 exec()
如果要將執行的回傳結果儲存下來,可加入第二個參數存值。
exec( "{command}", $output )

但有時候 $output 只有空值,查了些資料後得到如下結論:

  • 第二個參數 $output 只會抓 STDOUT 的內容
  • 如果回傳結果是 error messages,則會是屬於 STDERR,因此 $output 抓不到
  • 在 command 後加上 2>&1 可將 STDERR 轉成 STDOUT

Ref.

  1. http://stackoverflow.com/questions/3099617/php-capturing-the-command-output
  2. http://blog.longwin.com.tw/2013/06/php-system-exec-shell_exec-diff-2013/
PHP