Run php in cli mode

PHP CLI

If you want to run the PHP script directly without the leading php command then add #!/usr/local/bin/php as the first line as shown below.

 $ hello.php -jailshell: hello.php: command not found 

Whoops, command not found. The current directory isn’t in the path. This can be fixed by including the current directory in the path like this, PATH=$PATH:. . Another alternative is to specify the current directory like this, ./hello.php .

The following examples use both methods to run scripts in the current directory.

 $ PATH=$PATH:. $ hello.php -jailshell: hello.php: Permission denied 

Ugh, Permission denied. No worries, the script just needs to be made executable with the chmod command.

 $ ls -l hello.php -rw-r--r-- 1 user user Jan 25 09:11 hello.php $ chmod 744 hello.php $ ls -l hello.php -rwx r--r-- 1 user user Jan 25 09:11 hello.php* $ ./hello.php Content-type: text/html hello 

WARNING: If you see Content-type: text/html then you are not running the CLI version of PHP. I got this error because I incorrectly pointed to /usr/bin/php instead of usr/local/bin/php.

Читайте также:  Python blob to image

Anyway. if you must continue running the cgi version of php then you can suppress the Content-type: text/html header by running PHP in quiet mode with #!/usr/bin/php -q .

Run the PHP script again and all looks good. Again, the -q switch is only required because I was incorrectly running the cgi version of PHP instead of the cli version. Hostgator makes it transparent and easy to use the CLI version, but I still managed to mess it up.

 $ hello.php hello 

SAPI PHP Server API

PHP has a special variant that is tweaked to work from the command-line. The php -v version command will clearly show if your are running the cli version.

 $ which php /usr/local/bin/php $ php -v PHP 5.4.45 (cli) (built: Apr 17 2017 15:59:08) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v10.2.4, Copyright (c) 2002-2018, by ionCube Ltd. with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies $ /usr/bin/php -v PHP 5.4.45 (cgi-fcgi) (built: Apr 17 2017 15:59:08) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v10.2.4, Copyright (c) 2002-2018, by ionCube Ltd. with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies 

In the above example php -v , located at /usr/local/bin/php, returns cli but /usr/bin/php -v returns cgi-fcgi. My mistake above was to incorrectly type #!/usr/bin/php instead of #!/user/local/bin/php .

CGI-FCGI vs CLI

  • The CGI mode prints a Content-type: text/html header, the CLI does not. The CLI behavior is preferable from the command line. When I was incorrectly using the CGI version, I worked around the issue by running PHP in quite mode, #!/usr/bin/php -q . Again, this is not needed if you are using CLI mode.
  • The getcwd() function works differently between the two modes. In CGI mode getcwd() returns the directory where the main is located, often the directory where index.php is. This is intuitively correct when serving web pages, but it’s not what is expected from the command line. From the Command Line the PHP script’s main is often buried at some-random-path/bin/script.php. So, in CGI mode the function getcwd() returns some-random-path/bin, when you really want the directory you’re currently in. In CLI mode getcwd() behaves exactly as you expect and returns the same value that the Unix pwd command returns. I worked around this issue by using the variable $_SESSION[‘PWD’] which returns the expected value.
  • When in CGI mode the error messages have embedded HTML tags. When in CLI mode the HTML tags are suppressed.

php_sapi_name

The php_sapi_name() function returns cli or something else, such as cgi-fcgi. The following example checks the Server API (SAPI) at the start of the command-line script. If the SAPI is not cli then the script aborts with an error message.

 $ sapi_test.php Correct SAPI!! $ /usr/bin/php sapi_test.php Content-type: text/html Wrong Server API (SAPI): 'cgi-fcgi' 

Version of PHP

The php -v command returns the CLI version of PHP. Note, Apache httpd server may be configured to use a different version.

 $ php -v PHP 5.4.45 (cli) (built: Apr 17 2017 15:59:08) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v10.2.4, Copyright (c) 2002-2018, by ionCube Ltd. with Zend Guard Loader v3.3, Copyright (c) 1998-2013, by Zend Technologies 

Which PHP am I using

The which command returns the location of the program. Sometimes the path is just a link. The ls -l command returns where the link points to. The ls -d command returns the PHP versions that are available on the the system.

 $ which php /usr/local/bin/php $ ls -l /usr/local/bin/php lrwxrwxrwx 1 root root 18 Jun 26 2018 /usr/local/bin/php -> /opt/php54/bin/php* $ ls -d /opt/php* /opt/php52/ /opt/php53/ /opt/php54/ /opt/php55/ /opt/php56/ /opt/php70/ /opt/php71/ /opt/php_with_imap_client/ /opt/phpcur@ /opt/phpedge@ /opt/phpstable@ 

Change PHP Version

One way to change the PHP version is to create an alias. In the following example php -v returns the current version. The alias php=»/opt/php71/bin/php» command defines an alias for php. The php -v command shows the PHP version changed to 7.1.14. The unalias command removes the alias and the PHP version changes back to 5.4.45.

 $ php -v PHP 5.4.45 (cli) (built: Apr 17 2017 15:59:08) $ alias php="/opt/php71/bin/php" $ php -v PHP 7.1.14 (cli) (built: Feb 23 2018 18:28:03) ( NTS ) $ unalias php $ php -v PHP 5.4.45 (cli) (built: Apr 17 2017 15:59:08) 

Another way to change the version is to add the desired version to the beginning of the PATH using the assignment PATH=/opt/php56/bin:$PATH . The php -v and which php show the version correctly changed. One way to undo this change is to use the sed command as shown in the example. Another way is to exit the terminal and start a new terminal.

I prefer this method. The alias doesn’t seem to always work for me.

 $ PATH=/opt/php56/bin:$PATH $ which php /opt/php56/bin/php $ php -v PHP 5.6.30 (cli) (built: Mar 27 2017 11:48:20) $ echo $PATH /opt/php56/bin/php:/usr/local/jdk/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin $ PATH=`echo $PATH |sed -e 's/[^:]*://'` $ echo $PATH /usr/local/jdk/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin $ php -v PHP 5.4.45 (cli) (built: Apr 17 2017 15:59:08) 

The above alias and PATH statements were run from the command line and the PHP version change will be lost when the terminal is closed. To make these changes permanent put them into the ~/.bashrc startup script.

Script Tags

The PHP closing script tag ?> is not required in any PHP script. It is often best not to close a script because blank lines after the close are transmitted to the browser and may cause unexpected, difficult to debug, behavior. None of my example scripts have a closing tag.

Arguments $argv and $argc

PHP uses the global variables $argv and $argc for command-line arguments as shown in the following example.

Array $argv contains the list of arguments. The first item, $argv[0], contains the name of the PHP script. $argc indicates the number of arguments in $argv and is the same as count($argv) or sizeof($argv).

 $ hello.php cat dog hello argc = 3 count = 3 sizeof = 3 argv[0] = './hello.php' argv[1] = 'cat' argv[2] = 'dog' 

PHP Functions & Commands

  • file_exists — requires full path file_exists($pwd . DIRECTORY_SEPARATOR . $filename)
  • getcwd — returns the path to php’s «main» function.
  • $_SESSION[‘PWD’] — this is usually what I think pwd should be.
  • copy — copies just one file at a time. You need to roll your own cp -r

Recent Posts

Источник

How to Run PHP Code Using CLI and Built-in Web Server: A Comprehensive Guide

Learn how to run PHP code using the Command Line Interface (CLI) and built-in web server for faster development and testing. This comprehensive guide covers key points, important tips, and helpful advice.

  • Understanding the CLI SAPI and built-in web server
  • Running PHP scripts using the CLI
  • How to use PHP CLI Command Line & Built In Web Server
  • Installing and configuring the CLI on GNU/Linux systems
  • Debugging PHP code using the CLI
  • Advantages and disadvantages of using the CLI
  • Other useful code examples for running PHP code using CLI
  • Conclusion
  • How to run PHP from CLI?
  • How do I run PHP on a server?
  • What is PHP CLI server?
  • How to enable PHP CLI?

Running PHP code using the Command Line Interface (CLI) and the built-in web server is a convenient way to develop and test PHP code. In this blog post, we will explore the key points, important points, and helpful points of running PHP code using the CLI and built-in web server.

Understanding the CLI SAPI and built-in web server

The CLI SAPI provides a built-in web server for running PHP code, but it only runs one single-threaded process which can lead to application stall if a request is blocked. The built-in web server is useful for testing PHP applications without the need for a full web server setup.

Running PHP scripts using the CLI

To run PHP scripts using the CLI, pass the PHP code to execute directly on the command line. Use the WHICH command to show the location of the PHP binary executable in *nix systems, which is the path to use as the first line in your PHP shell script. Use command-line options like “-a”, “-c

| ”, “-n”, and “-d foo[=bar]” to customize the PHP environment.

Here is an example of running a PHP script using the CLI:

How to use PHP CLI Command Line & Built In Web Server

In this video I show you How to use the php cli command line and the built in web server Duration: 12:21

Installing and configuring the CLI on GNU/Linux systems

To use interactive mode enabled on GNU/Linux on distros Debian/Ubuntu/LinuxMint, install “php -cli” and “php -readline” packages from official. ServerPilot installs multiple versions of PHP on your server so there are multiple PHP executables available to run. The CLI SAPI is enabled by default using “–enable-cli,” but may be disabled using the “–disable-cli” option when running “./configure.”

Debugging PHP code using the CLI

There are several ways to start a PHP CLI debugging session, including using the “phpdbg” command and the Xdebug extension. If a script (with the exec command) is loaded more than once by the same user at the same time, the server will freeze.

Here is an example of starting a PHP CLI debugging session using the Xdebug extension:

$ php -dxdebug.remote_autostart=1 -dxdebug.remote_mode=req -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_port=9000 -dxdebug.remote_connect_back=0 my_script.php 

Advantages and disadvantages of using the CLI

Advantages of using the CLI include faster development and testing, ability to run scripts without a web server, and easy access to installed PHP extensions. Disadvantages of using the CLI include lack of access to web server resources like session handling and cookies, and limited debugging options.

Other useful code examples for running PHP code using CLI

In Php , for instance, run php server code sample

cd path/to/your/app php -S localhost:8000

In Php case in point, run a server php terminal code example

$ cd ~/public_html $ php -S localhost:8000

In Php case in point, php run command line code example

In Php case in point, start php cli code sample

In Php as proof, cmd run php file code sample

// terminal run php file // First run "php" in the terminal, in the path directory php // If php works there. Then : php path_to_file/file_name.php//This will run your php and print stuff to your console in plain text. //It's useful for a range of testing and data maniplation purposes. //And has the full php cammand line. So potential is up to your imagination.
To execute a php script, use the PHP Command Line interface(CLI) and specify the file name of the script in the following way: php script.php

Conclusion

Running PHP code using the CLI and built-in web server is a useful tool for developers and testers. By understanding the key points, important points, and helpful points of running PHP code using the CLI and built-in web server, developers can improve their development workflow and testing process.

Источник

Оцените статью