4.3以降でPHPは Command Line Interfaceを意味する CLIという名前の新しいSAPI型 (Server Application Programming Interface)をサポートします。 名前から分かるように、このSAPI型は、 PHPによるシェル(またはデスクトップ)アプリケーショ ンの開発を主な対象としています。 CLI SAPIと他のSAPIの間には、 いくつかの違いがあります。本章では、これらについて詳細を説明します。
CLI SAPIは、当初PHP 4.2.0でリ リースされましたが、この時点では実験的なステータスにあったため、 ./configureを実行する際に、明示的に --enable-cliを指定することにより、有効とする必要 がありました。PHP 4.3.0以降、 CLI SAPIはもはや実験的なステータスではなくなりま した。このため、 常に構築され、 php(Windowsではphp.exeと いう名前となります)バイナリとしてインストールされます。
CLI SAPIを他のSAPIと比べた時 の大きな違いを以下に示します。
CGI SAPIと異なり、ヘッダが出力されません。
CGI SAPIは、HTTPヘッダの出力を抑制する機能を 提供していますが、等価な機能はCLI SAPIではサ ポートされていません。
以下に示すいくつかのphp.iniディレクティブは、CLI SAPIにより上書きされます。これは、シェル環境では意味が ないためです。
表 24-1上書きされるphp.iniのディレクティブ
ディレクティブ | CLI SAPIのデフォルト値 | コメント |
---|---|---|
html_errors | FALSE | エラーメッセージに含まれるHTMLタグはシェ ル上では意味がなく、可読性をかなり低下させるため、このディ レクティブはデフォルトでFALSEとなっています。 |
implicit_flush | TRUE | print(), echo()および 関連するものによる全ての出力は、直ちに出力され、バッファに キャッシュされないことが望ましいと言えます。この場合でも、 標準出力を保留または操作したい場合には、 output bufferingを使 用することが可能です。 |
max_execution_time | 0 (unlimited) | シェル環境では、PHPを際限なく使用できる ようにするために、最大実行時間の制限は無しに設定されていま す。Web用アプリケーションは数秒単位で実行されるよう作られて いますが、シェルアプリケーションの実行時間は、これよりかな り長くなる傾向があります。 |
register_argc_argv | TRUE | CLI SAPIを使用している場合、グローバル PHP変数$argc (アプリケーションに渡される引数の数)と $argv (引数の値の配列)は常に登録され、 適切な値が代入されます。 |
注意 これらのディレクティブは、設定ファイルphp.iniまたはカスタム設 定ファイル(指定した場合のみ)で他の値に初期化できません。この制 限は、これらのデフォルト値が全ての設定ファイルをパースした後に 適用されるためです。しかし、これらの値は実行時に変更することが 可能です。 (上記のディレクティブの全てについてこれが当てはまるわけではあり ません。例えば、register_argc_argv)
シェル環境での動作を容易とするために、以下の定数が定義されていま す。
表 24-2CLI固有の定数
定数 | 説明 | |
---|---|---|
STDIN |
stdinへのオープン済みのストリーム。これ
により、以下のようにオープンする必要がなくなります。
| |
STDOUT |
stdoutへのオープン済みのストリーム。これ
により、以下のようにオープンする必要がなくなります。
| |
STDERR |
stderrへのオープン済みのストリーム。これ
により、以下のようにオープンする必要がなくなります。
|
上記のように、stderrのようなストリームを自分 でオープンする必要はなく、以下のようにストリームリソースの代わり に定数を使用するだけでかまいません。
php -r 'fwrite(STDERR, "stderr\n");' |
CLI SAPIは、実行されるスクリプトのディレクト リにカレントディレクトリを変更しません !
CGI SAPIとの違いを示す例を以下に示します。
<?php /* Our simple test application */ echo getcwd(), "\n"; ?> |
CGI版により実行した場合、出力は以下のようにな ります。
$ pwd /tmp $ php-cgi -f another_directory/test.php /tmp/another_directory |
CLI SAPIを使用した場合の出力は次のようになり ます。
$ pwd /tmp $ php -f another_directory/test.php /tmp |
注意 CGI SAPIは、このCLI SAPI の動作をコマンドライン実行時のスイッチ-Cによ りサポートしています。
PHPバイナリにより提供されるコマンドラインオプショ ンの一覧は、-hスイッチを指定して PHPを実行することによりいつでも調べることができ ます。
Usage: php [options] [-f] <file> [args...] php [options] -r <code> [args...] php [options] [-- args...] -s Display colour syntax highlighted source. -w Display source with stripped comments and whitespace. -f <file> Parse <file>. -v Version number -c <path>|<file> Look for php.ini file in this directory -a Run interactively -d foo[=bar] Define INI entry foo with value 'bar' -e Generate extended information for debugger/profiler -z <file> Load Zend extension <file>. -l Syntax check only (lint) -m Show compiled in modules -i PHP information -r <code> Run PHP <code> without using script tags <?..?> -h This help args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin |
CLI SAPIは、実行するPHPコード を取得するために三種類の異なる手段をサポートしています。
PHPに特定のファイルの実行を指示する。
php my_script.php php -f my_script.php |
実行するPHPコードをコマンドラインで直接指定する。
php -r 'print_r(get_defined_constants());' |
注意 この例をよくみて下さい。開始/終了タグがありません! -rスイッチを使用した場合、これらのタグは不要 となります。これらのタグを使用するとパーサエラーを発生します。
実行するPHPコードを標準入力 (stdin)で指定する。
これは強力な機能で、以下の(仮想的な)例に示すように、動的に PHPコードを生成し、実行バイナリに入力すること ができます。
$ some_application | some_filter | php | sort -u >final_output.txt |
他のシェルアプリケーションのように、PHPバイナリ に引数を指定することができるだけでなく、PHPスク リプトがこの引数を取得することも可能です。スクリプトに指定できる引 数の数はPHPによる制限を受けません。 (シェルは指定可能な文字数の最大値を設定しています。通常、この制限値 を越えることはできません。) スクリプトに指定した引数は、グローバル 配列$argvでアクセス可能です。 添字0は、常にスクリプト名が含まれています。 ( PHPコードが標準入力またはコマンドラインスイッ チ-rにより指定された場合、スクリプト名は -となります。) 登録される第2のグローバル変数は$argcで、 (スクリプトに指定された引数の数ではなく 、)配列$argvの要素数が含まれます。
スクリプトに指定する引数が文字-で始まっていない 限り、特に留意すべきことはありません。スクリプトに指定する引数が文 字-で始まる場合、PHP自体がこ れをパースする必要があるとみなすため、問題を発生します。 これを防止するため、引数リストセパレータ--を使用 して下さい。PHPにパースされる引数の後に このセパレータを置くと、その後の全ての引数はそのままパースされずに スクリプトに渡されます。
# This will not execute the given code but will show the PHP usage $ php -r 'var_dump($argv);' -h Usage: php [options] [-f] <file> [args...] [...] # This will pass the '-h' argument to your script and prevent PHP from showing it's usage $ php -r 'var_dump($argv);' -- -h array(2) { [0]=> string(1) "-" [1]=> string(2) "-h" } |
また、PHPをシェルスクリプトとして使用する他の 手段があります。最初の行が#!/usr/bin/phpで始まり、 PHPの開始/終了タグの中に通常の PHPコードが続くスクリプトを書き、適当なファイル 実行属性を設定することが可能です。この方法は、通常のシェル/Perlスク リプトと同様に実行することができます。
#!/usr/bin/php <?php var_dump($argv); ?> |
$ chmod 755 test $ ./test -h -- foo array(4) { [0]=> string(6) "./test" [1]=> string(2) "-h" [2]=> string(2) "--" [3]=> string(3) "foo" } |
表 24-3コマンドラインオプション
オプション | 説明 | |||
---|---|---|---|---|
-s |
カラー構文ハイライト表示されたソースを表示します。 このオプションは、ファイルをパースし、HTML ハイライト表示版のファイルを生成し、標準出力に書き出す内部機 構を使用します。行うのは、 <code> [...] </code>のブロック を生成することだけで、HTMLヘッダは出力され ないことに注意して下さい。
| |||
-w |
コメントと空白文字を削除してソースを表示します。
| |||
-f |
Parses and executed the given filename to the -f option. This switch is optional and can be left out. Only providing the filename to execute is sufficient. | |||
-v |
PHP, PHP SAPI, Zendのバージョンを標準出力に出力します。例:
| |||
-c |
このオプションを使用することにより、php.iniを探すディレクト リを指定したり、カスタマイズされたINIファ イル(php.iniという名前である必要はありません)を直接指定する ことが可能です。例:
| |||
-a |
PHPを対話的に実行します。 | |||
-d |
This option allows to set a custom value for any of the configuration directives allowed in php.ini. The syntax is:
例:
| |||
-e |
デバッガ/プロファイラ用の拡張情報を出力します。 | |||
-z |
Load Zend extension. If only a filename is given, PHP tries to load this extension from the current default library path on your system (usually specified /etc/ld.so.conf on Linux systems). Passing a filename with an absolute path information will not use the systems library search path. A relative filename with a directory information will tell PHP only to try to load the extension relative to the current directory. | |||
-l |
This option provides a convenient way to only perform a syntax check on the given PHP code. On succes, the text No syntax errors detected in <filename> is written to standard output and the shell return code is 0. On failure, the text Errors parsing <filename> in addition to the internal parser error message is written to standard output and the shell return code is set to 255. This option won't find fatal errors (like undefined functions). Use -f if you would like to test for fatal errors too.
| |||
-m |
Using this option, PHP prints out the built in (and loaded) PHP and Zend modules:
| |||
-i | This command line option calls phpinfo(), and prints out the results. If PHP is not working well, it is advisable to make a php -i and see if any error messages are printed out before or in place of the information tables. Beware that the output is in HTML and therefore quite huge. | |||
-r |
This option allows execution of PHP right from within the command line. The PHP start and end tags (<?php and ?>) are not needed and will cause a parser errors.
| |||
-h | With this option, you can get information about the actual list of command line options and some one line descriptions about what they do. |
PHP実行バイナリは、Webサーバから完全に独立してPHPスクリプトを実行す るために使用することができます。Unixシステムを使用している場合、実 行可能とするために、PHPスクリプトの先頭に特別な一行を追加する必要が あります。これにより、システムがそのスクリプトを実行するプログラム を知ることができます。 Windows環境では、.phpファイルのダブルクリックオ プションにphp.exeを関連づけることができます。 または、PHPによりスクリプトを実行するバッチファイルを作成することも 可能です。Unix上で動作させるためにスクリプトに追加された先頭行は、 Windows環境での動作に悪影響を与えません。このため、この手法により、 黒須プラットフォーム環境で動作するプログラムを書くことができます。 コマンドラインPHPプログラムの書方の簡単な例を以下に示します。
In the script above, we used the special first line to indicate, that this file should be run by PHP. We work with a CLI version here, so there will be no HTTP header printouts. There are two variables you can use while writing command line applications with PHP: $argc and $argv. The first is the number of arguments plus one (the name of the script running). The second is an array containing the arguments, starting with the script name as number zero ($argv[0]).
In the program above we checked if there are less or more than one arguments. Also if the argument was --help, -help, -h or -?, we printed out the help message, printing the script name dynamically. If we received some other argument we echoed that out.
If you would like to run the above script on Unix, you need to make it executable, and simply call it as script.php echothis or script.php -h. On Windows, you can make a batch file for this task:
Assuming, you named the above program as script.php, and you have your php.exe in c:\php\php.exe this batch file will run it for you with your added options: script.bat echothis or script.bat -h.
See also the Readline extension documentation for more functions you can use to enhance your command line applications in PHP.