Home Resources Articles Command Line Scripting in PHP. PHP is a great language to develop web applications rapidly and economically. Many of us know this. But what many of us were unaware of is that PHP can also be run as a command-line script like C, C++, and Java, etc.
As of version 4.3. 0, PHP supports a new SAPI type (Server Application Programming Interface) named CLI which means Command Line Interface. As the name implies, this SAPI type main focus is on developing shell (or desktop as well) applications with PHP.
Use the php_sapi_name()
function.
if (php_sapi_name() == "cli") {
// In cli-mode
} else {
// Not in cli-mode
}
Here are some relevant notes from the docs:
php_sapi_name — Returns the type of interface between web server and PHP
Although not exhaustive, the possible return values include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, cli-server, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.
In PHP >= 4.2.0, there is also a predefined constant, PHP_SAPI
, that has the same value as php_sapi_name()
.
This will always work. (If the PHP version is 4.2.0 or higher)
define('CLI', PHP_SAPI === 'cli');
Which makes it easy to use at the top of your scripts:
<?php PHP_SAPI === 'cli' or die('not allowed');
Here is Drupal 7 implementation: drupal_is_cli():
function drupal_is_cli() {
return (!isset($_SERVER['SERVER_SOFTWARE']) && (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));
}
However Drupal 8 recommends using PHP_SAPI === 'cli'
I think
$_SERVER['REMOTE_ADDR']
will not be populated from the CLI.
Also, all the HTTP_* keys in the $_SERVER superglobal won't be populated from the CLI, or do it the right way hop just mentioned :-)
The documentation page for php_sapi
_name clearly states how it works:
Returns a lowercase string that describes the type of interface (the Server API, SAPI) that PHP is using....
Although not exhaustive, the possible return values include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.
I'm not sure why hop doesn't think that PHP is for serious programmers (I'm a serious programmer, and I use PHP daily), but if he wants to help clarify the documentation then perhaps he can audit all possible web servers that PHP can run on and determine the names of all possible interface types for each server. Just make sure to keep that list updated as new web servers and interfaces are added.
Also, Bobby said:
I'm intrigued as to why the doc. example inspects the first 3 characters, whilst the description states the string should be exactly "CGI"
The description for the example states:
This example checks for the substring cgi because it may also be cgi-fcgi.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With