PHPのお勉強!

PHP TOP

PHP をコマンドラインから使用する

目次

はじめに

CLI SAPI の主な目的は、シェルアプリケーションを PHP で開発することです。CLI SAPI とその他の SAPI の間にはちょっとした違いがあり、 それを本章で説明します。CLICGI は多くの共通点がありますが、別の SAPI であるということも覚えておきましょう。

CLI SAPI は、 --enable-cli でデフォルトで有効となっています。 ./configure の際に --disable-cli オプションを指定して無効にすることもできます。

CLI/CGI バイナリの名前、位置、そして存在するかどうかは PHP がどのようにインストールされているかによって異なります。デフォルトで make を実行したときには、CGICLI が両方ビルドされて、それぞれ PHP ソースディレクトリの sapi/cgi/php-cgisapi/cli/php にできあがります。 両方とも php という名前であることに注意しましょう。 make install のときにどうなるかは、configure 行に依存します。 configure で例えば apxs のような SAPI モジュールが選択された場合、または --disable-cgi が指定された場合、 make install によって CLI{PREFIX}/bin/php にコピーされます。さもなければ CGI がそこにコピーされます。 つまり、たとえば configure で --with-apxs を指定すると、make install での CLI のコピー先は {PREFIX}/bin/php となります。 既にインストールされている CGI バイナリを上書きしたい場合には、 make install の後に make install-cli を実行してください。あるいは configure で --disable-cgi を指定することもできます。

注意:

--enable-cli--enable-cgi の両方がデフォルトで有効になっています。そのため、configure で --enable-cli を指定したからといって、make install{PREFIX}/bin/php にコピーされるのが必ずしも CLI になるとは限りません。

Windows 版の CLI はメインフォルダ内で php.exe という名前で配布されます。CGI バージョンは、php-cgi.exe として配布されます。さらに configure で --enable-cli-win32 を指定すると、新しく php-win.exe というファイルが配布されます。 これは CLI バージョンとほぼ同じですが、何も出力しないため、コンソールは必要ありません。

注意: 自分の SAPI は何か?

シェルで php -v をタイプすると、 phpCGI なのか CLI なのかわかります。 php_sapi_name() と定数 PHP_SAPI も参照ください。

注意:

Unix の man ページは、 シェル環境から man php とすれば見ることができます。

add a note

User Contributed Notes 33 notes

up
117
sep16 at psu dot edu
12 years ago
You can easily parse command line arguments into the $_GET variable by using the parse_str() function.

<?php

parse_str
(implode('&', array_slice($argv, 1)), $_GET);

?>

It behaves exactly like you'd expect with cgi-php.

$ php -f somefile.php a=1 b[]=2 b[]=3

This will set $_GET['a'] to '1' and $_GET['b'] to array('2', '3').

Even better, instead of putting that line in every file, take advantage of PHP's auto_prepend_file directive. Put that line in its own file and set the auto_prepend_file directive in your cli-specific php.ini like so:

auto_prepend_file = "/etc/php/cli-php5.3/local.prepend.php"

It will be automatically prepended to any PHP file run from the command line.
up
29
apmuthu at usa dot net
7 years ago
Adding a pause() function to PHP waiting for any user input returning it:

<?php
function pause() {
$handle = fopen ("php://stdin","r");
do {
$line = fgets($handle); } while ($line == '');
fclose($handle);
return
$line;
}
?>
up
24
ohcc at 163 dot com
8 years ago
use " instead of ' on windows when using the cli version with -r

php -r "echo 1"
-- correct

php -r 'echo 1'
PHP Parse error: syntax error, unexpected ''echo' (T_ENCAPSED_AND_WHITESPACE), expecting end of file in Command line code on line 1
up
21
PSIKYO at mail dot dlut dot edu dot cn
11 years ago
If you edit a php file in windows, upload and run it on linux with command line method. You may encounter a running problem probably like that:

[root@ItsCloud02 wsdl]# ./lnxcli.php
Extension './lnxcli.php' not present.

Or you may encounter some other strange problem.
Care the enter key. In windows environment, enter key generate two binary characters '0D0A'. But in Linux, enter key generate just only a 'OA'.
I wish it can help someone if you are using windows to code php and run it as a command line program on linux.
up
22
lucas dot vasconcelos at gmail dot com
17 years ago
Just another variant of previous script that group arguments doesn't starts with '-' or '--'

<?php
function arguments($argv) {
$_ARG = array();
foreach (
$argv as $arg) {
if (
ereg('--([^=]+)=(.*)',$arg,$reg)) {
$_ARG[$reg[1]] = $reg[2];
} elseif(
ereg('^-([a-zA-Z0-9])',$arg,$reg)) {
$_ARG[$reg[1]] = 'true';
} else {
$_ARG['input'][]=$arg;
}
}
return
$_ARG;
}

print_r(arguments($argv));
?>

$ php myscript.php --user=nobody /etc/apache2/*
Array
(
[input] => Array
(
[0] => myscript.php
[1] => /etc/apache2/apache2.conf
[2] => /etc/apache2/conf.d
[3] => /etc/apache2/envvars
[4] => /etc/apache2/httpd.conf
[5] => /etc/apache2/mods-available
[6] => /etc/apache2/mods-enabled
[7] => /etc/apache2/ports.conf
[8] => /etc/apache2/sites-available
[9] => /etc/apache2/sites-enabled
)

[user] => nobody
)
up
25
drewish at katherinehouse dot com
19 years ago
When you're writing one line php scripts remember that 'php://stdin' is your friend. Here's a simple program I use to format PHP code for inclusion on my blog:

UNIX:
cat test.php | php -r "print htmlentities(file_get_contents('php://stdin'));"

DOS/Windows:
type test.php | php -r "print htmlentities(file_get_contents('php://stdin'));"
up
26
frankNospamwanted at. toppoint dot. de
10 years ago
Parsing commandline argument GET String without changing the PHP script (linux shell):
URL: index.php?a=1&b=2
Result: output.html

echo "" | php -R 'include("index.php");' -B 'parse_str($argv[1], $_GET);' 'a=1&b=2' >output.html

(no need to change php.ini)

You can put this
echo "" | php -R 'include("'$1'");' -B 'parse_str($argv[1], $_GET);' "$2"
in a bash script "php_get" to use it like this:
php_get index.php 'a=1&b=2' >output.html
or directed to text browser...
php_get index.php 'a=1&b=2' |w3m -T text/html
up
21
Anonymous
3 years ago
We can pass many arguments directly into the hashbang line.
As example many ini setting via the -d parameter of php.
---
#!/usr/bin/php -d memory_limit=2048M -d post_max_size=0
phpinfo();
exit;
---
./script | grep memory
memory_limit => 2048M => 2048M
---
But we can also use this behaviour into a second script, so it call the first as an interpreter, via the hashbang:
---
#!./script arg1 arg2 arg3
---
However the parameters are dispatched in a different way into $argv

All the parameters are in $argv[1], $argv[0] is the interpreter script name, and $argv[1] is the caller script name.

To get back the parameters into $argv, we can simply test if $argv[1] contains spaces, and then dispatch again as normal:

#!/usr/bin/php -d memory_limit=2048M -d post_max_size=0
<?php
var_dump
($argv);
if (
strpos($argv[1], ' ') !== false){
$argw = explode(" ", $argv[1]);
array_unshift($argw, $argv[2]);
$argv = $argw;
}
var_dump($argv); ?>
---
array(3) {
[0]=>
string(8) "./script"
[1]=>
string(15) "arg1 arg2 arg3 "
[2]=>
string(14) "./other_script"
}
array(4) {
[0]=>
string(8) "./other_script"
[1]=>
string(4) "arg1"
[2]=>
string(4) "arg2"
[3]=>
string(4) "arg3"
}
---
This will maintain the same behaviour in all cases and allow to even double click a script to call both parameters of another script, and even make a full interpreter language layer. The other script doesn't has to be php. Take care of paths.
up
26
Kodeart
13 years ago
Check directly without calling functions:
<?php
if (PHP_SAPI === 'cli')
{
// ...
}
?>

You can define a constant to use it elsewhere
<?php
define
('ISCLI', PHP_SAPI === 'cli');
?>
up
20
monte at ispi dot net
21 years ago
I had a problem with the $argv values getting split up when they contained plus (+) signs. Be sure to use the CLI version, not CGI to get around it.

Monte
up
32
ben at slax0rnet dot com
20 years ago
Just a note for people trying to use interactive mode from the commandline.

The purpose of interactive mode is to parse code snippits without actually leaving php, and it works like this:

[root@localhost php-4.3.4]# php -a
Interactive mode enabled

<?php echo "hi!"; ?>
<note, here we would press CTRL-D to parse everything we've entered so far>
hi!
<?php exit(); ?>
<ctrl-d here again>
[root@localhost php-4.3.4]#

I noticed this somehow got ommited from the docs, hope it helps someone!
up
21
OverFlow636 at gmail dot com
19 years ago
I needed this, you proly wont tho.
puts the exicution args into $_GET
<?php
if ($argv) {
foreach (
$argv as $k=>$v)
{
if (
$k==0) continue;
$it = explode("=",$argv[$i]);
if (isset(
$it[1])) $_GET[$it[0]] = $it[1];
}
}
?>
up
24
thomas dot harding at laposte dot net
16 years ago
Parsing command line: optimization is evil!

One thing all contributors on this page forgotten is that you can suround an argv with single or double quotes. So the join coupled together with the preg_match_all will always break that :)

Here is a proposal:

#!/usr/bin/php
<?php
print_r
(arguments($argv));

function
arguments ( $args )
{
array_shift( $args );
$endofoptions = false;

$ret = array
(
'commands' => array(),
'options' => array(),
'flags' => array(),
'arguments' => array(),
);

while (
$arg = array_shift($args) )
{

// if we have reached end of options,
//we cast all remaining argvs as arguments
if ($endofoptions)
{
$ret['arguments'][] = $arg;
continue;
}

// Is it a command? (prefixed with --)
if ( substr( $arg, 0, 2 ) === '--' )
{

// is it the end of options flag?
if (!isset ($arg[3]))
{
$endofoptions = true;; // end of options;
continue;
}

$value = "";
$com = substr( $arg, 2 );

// is it the syntax '--option=argument'?
if (strpos($com,'='))
list(
$com,$value) = split("=",$com,2);

// is the option not followed by another option but by arguments
elseif (strpos($args[0],'-') !== 0)
{
while (
strpos($args[0],'-') !== 0)
$value .= array_shift($args).' ';
$value = rtrim($value,' ');
}

$ret['options'][$com] = !empty($value) ? $value : true;
continue;

}

// Is it a flag or a serial of flags? (prefixed with -)
if ( substr( $arg, 0, 1 ) === '-' )
{
for (
$i = 1; isset($arg[$i]) ; $i++)
$ret['flags'][] = $arg[$i];
continue;
}

// finally, it is not option, nor flag, nor argument
$ret['commands'][] = $arg;
continue;
}

if (!
count($ret['options']) && !count($ret['flags']))
{
$ret['arguments'] = array_merge($ret['commands'], $ret['arguments']);
$ret['commands'] = array();
}
return
$ret;
}

exit (
0)

/* vim: set expandtab tabstop=2 shiftwidth=2: */
?>
up
14
phpnotes at ssilk dot de
22 years ago
To hand over the GET-variables in interactive mode like in HTTP-Mode (e.g. your URI is myprog.html?hugo=bla&bla=hugo), you have to call

php myprog.html '&hugo=bla&bla=hugo'

(two & instead of ? and &!)

There just a little difference in the $ARGC, $ARGV values, but I think this is in those cases not relevant.
up
26
notreallyanaddress at somerandomaddr dot com
15 years ago
If you want to be interactive with the user and accept user input, all you need to do is read from stdin.

<?php
echo "Are you sure you want to do this? Type 'yes' to continue: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(
trim($line) != 'yes'){
echo
"ABORTING!\n";
exit;
}
echo
"\n";
echo
"Thank you, continuing...\n";
?>
up
16
jeff at noSpam[] dot genhex dot net
22 years ago
You can also call the script from the command line after chmod'ing the file (ie: chmod 755 file.php).

On your first line of the file, enter "#!/usr/bin/php" (or to wherever your php executable is located). If you want to suppress the PHP headers, use the line of "#!/usr/bin/php -q" for your path.
up
15
roberto dot dimas at gmail dot com
19 years ago
One of the things I like about perl and vbscripts, is the fact that I can name a file e.g. 'test.pl' and just have to type 'test, without the .pl extension' on the windows command line and the command processor knows that it is a perl file and executes it using the perl command interpreter.

I did the same with the file extension .php3 (I will use php3 exclusivelly for command line php scripts, I'm doing this because my text editor VIM 6.3 already has the correct syntax highlighting for .php3 files ).

I modified the PATHEXT environment variable in Windows XP, from the " 'system' control panel applet->'Advanced' tab->'Environment Variables' button-> 'System variables' text area".

Then from control panel "Folder Options" applet-> 'File Types' tab, I added a new file extention (php3), using the button 'New' and typing php3 in the window that pops up.

Then in the 'Details for php3 extention' area I used the 'Change' button to look for the Php.exe executable so that the php3 file extentions are associated with the php executable.

You have to modify also the 'PATH' environment variable, pointing to the folder where the php executable is installed

Hope this is useful to somebody
up
15
Anonymous
14 years ago
Using CLI (on WIN at least), some INI paths are relative to the current working directory. For example, if your error_log = "php_errors.log", then php_errors.log will be created (or appended to if already exists) in whatever directory you happen to be in at the moment if you have write access there. Instead of having random error logs all over the place because of this behavior, you may want to set error_log to a full path, perhaps to the php.exe directory.
up