Use php in shell

Shell run/execute php script with parameters

doesn’t work If someone could spell out to me what command to execute to get the php file to execute with set parameters, it would be much appreciated. If not, try to lead me the right direction.

A better answer than the accepted one: Use «php-cgi» executable instead of «php». See: stackoverflow.com/a/11965479/543738

5 Answers 5

$ php -q test.php foo bar Array ( [0] => test.php [1] => foo [2] => bar ) 

Ok, so I cannot receive the parameters via $_GET in php but through $argv, thanks. It took me a while to figure that out.

If you have webserver (not only just php interpreter installed, but LAMP/LNMP/etc) — just try this

wget -O - -q -t 1 "http://mysite.com/file.php?show=show_name" >/dev/null 2>&1 
  • « -O — » — (Letter «O», not zero!) redirect «downloaded html» to stdout
  • « >/dev/null 2>&1 » — redirect stdout & stderr output to nowhere
  • « -q » — quiet wget run
  • « -t 1 » — just 1 try to connect (not like default 20)

In PHP’s «exec» it’ll be smth like this:

function exec_local_url($url) < exec('/usr/bin/wget -O - -q -t 1 "http://'. $_SERVER['HTTP_HOST'] .'/' . addslashes($url) . '" >/dev/null 2>&1' ); > // . exec_local_url("file.php?show=show_name"); exec_local_url("myframework/seo-readable/show/show_name"); 

So, you don’t need to change your scripts to handle argc/argv, and may use $_GET as usually do.

Читайте также:  Найти корни квадратного уравнения python

If you want jobs runned in background — see for ex. Unix/Windows, Setup background process? from php code

I use approach with wget in my cron jobs; hope it helps.

Is there any difference between using wget and php commands when running jobs with exec regarding security or performace?

I sure security is the same. Performance — of course wget will be more slow but much handy as of said upper. With wget the request goes all hidden stages or request handling like apache/nginx/lighttpd caches, rewrites and so on. When in direct call of php you will blame so complex workaround,- i.e. it’s appliable not in all cases. Anyway, I think you just must take your current case, your brain, benchmarking tool and just shuffle it a good! 😉

Источник

How to Execute PHP Code in a Shell Environment: A Comprehensive Guide

Learn how to execute PHP code in a shell environment, including interactive shells, executing PHP files, using shell scripts, and executing shell commands in PHP. Get a comprehensive guide here.

  • Executing PHP Code in Interactive Shells
  • Executing PHP Files on the Command Line
  • How to Execute PHP Codes in Linux through Command Line
  • Executing PHP Code on the Command Line
  • Executing PHP Scripts with Shell Scripts or Cron Jobs
  • Executing Shell Commands in PHP
  • Other examples of simple shell code for executing PHP
  • Conclusion
  • How to run PHP from shell script?
  • How does PHP get executed?
  • How to run PHP interactive shell?

PHP is a popular programming language used for web development. It is a server-side language that is executed on a web server to generate dynamic web pages. However, PHP can also be executed in various environments, including shell environments. In this post, we will cover how to execute PHP code in a shell environment, including interactive shells, executing PHP files , using shell scripts, and executing shell commands in php .

Executing PHP Code in Interactive Shells

PHP code can be executed directly in an interactive shell using the -a option. This is useful for quick testing and debugging of PHP code. To start an interactive shell, open a terminal and type php -a . This will start the PHP interactive shell, where you can run PHP code and see the output in real-time.

On Windows, the readline extension must be enabled to use the interactive shell. To enable the readline extension, uncomment the following line in your php.ini file:

Executing PHP Files on the Command Line

PHP files can be executed using the command line by passing the file name as an argument. To execute a PHP file, open a terminal and type php filename.php . The PHP executable will execute the file and display the output in the terminal.

On Unix systems, the special #! first line should be added to PHP scripts to specify which program should run the script. For example, if the PHP executable is located at /usr/bin/php , the first line of the PHP script should be:

This tells the shell to use the PHP executable to execute the script.

When PHP is executed as a script, it runs as a separate process from the web server. This means that any changes to the PHP configuration, such as increasing the memory limit, must be made in the PHP configuration file for the command line interface.

How to Execute PHP Codes in Linux through Command Line

This video explains how to Execute PHP Codes through Command Line. PHP is designed for Duration: 6:04

Executing PHP Code on the Command Line

In addition to Executing PHP Files , PHP code can also be executed directly on the command line using the -r option. To execute PHP code on the command line, open a terminal and type php -r «echo ‘Hello, world!’;» . This will execute the PHP code and display the output in the terminal.

When Executing PHP Code on the command line, special variables are available that are not available when executing PHP in a web server environment. For example, the $argc and $argv variables contain the number of arguments passed to the script and an array of the arguments, respectively.

The -q option can be used with the PHP executable to execute PHP scripts as shell scripts. This option suppresses HTTP headers and other output that is not relevant when executing PHP code as a shell script.

Executing PHP Scripts with Shell Scripts or Cron Jobs

A PHP script can be made executable and run using a shell script or a Cron job. To make a PHP script executable , add the following line to the beginning of the script:

This tells the shell to use the PHP executable to execute the script. The script can then be made executable using the chmod command:

The script can be executed like any other shell script by typing ./script.php .

The CLI SAPI provides an interactive shell using the -a option if PHP is compiled with the —with-readline option. This allows you to run PHP code interactively and debug scripts from the command line.

The passthru() function can be used to execute a command and return all its data. This function is useful for executing shell commands from a php script and capturing their output.

Executing Shell Commands in PHP

The exec() function can be used to execute shell commands in PHP. This function takes a command as an argument and returns the output of the command. For example, to execute the ls command and display its output, you can use the following code:

$output = exec('ls'); echo $output; 

It is important to avoid executing user input as shell commands to prevent security vulnerabilities . Any user input should be validated and sanitized before being used in a shell command.

Permission issues may arise when Executing PHP Scripts as shell scripts. This is because the user that executes the script may not have the necessary permissions to access certain files or directories. It is important to ensure that the user that executes the script has the necessary permissions to perform the desired actions.

Other examples of simple shell code for executing PHP

In Php , in particular, run shell script from php file code sample

echo shell_exec('sh /home/scripts/fix-perm.sh'); 

In Php , in particular, execute php in terminal code sample

In Javascript case in point, execute php code sample

prepare('SELECT nom, couleur, calories FROM fruit WHERE calories < ? AND couleur = ?'); $sth->bindParam(1, $calories, PDO::PARAM_INT); $sth->bindParam(2, $couleur, PDO::PARAM_STR, 12); $sth->execute(); ?>

In Shell , for instance, execute php file from command line code sample

In Php , in particular, execute script php command line code sample

php php_file.php // or php php_file.php > result_file.txt // to display the result of echoes or dumps
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

Executing PHP code in a shell environment can be useful for quick testing and debugging. PHP code can be executed in interactive shells, executed as PHP files, executed on the command line, and executed with shell scripts or Cron jobs. Special variables and functions are available for executing PHP code in shell environments, but security vulnerabilities and permission issues should be considered. By following the guidelines in this post, you can execute PHP code in a shell environment with confidence and efficiency.

Источник

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