Asked  7 Months ago    Answers:  5   Viewed   29 times

How can I run a PHP script from the command line using the PHP interpreter which is used to parse web scripts?

I have a phpinfo.php file which is accessed from the web shows that German is installed. However, if I run the phpinfo.php from the command line using - php phpinfo.php and grep for German, I don't find it. So both phps are different. I need to run a script which the php on which German is installed.

How can I do this?

 Answers

45

UPDATE:

After misunderstanding, I finally got what you are trying to do. You should check your server configuration files; are you using apache2 or some other server software?

Look for lines that start with LoadModule php... There probably are configuration files/directories named mods or something like that, start from there.

You could also check output from php -r 'phpinfo();' | grep php and compare lines to phpinfo(); from web server.

To run php interactively:

(so you can paste/write code in the console)

php -a

To make it parse file and output to console:

php -f file.php

Parse file and output to another file:

php -f file.php > results.html

Do you need something else?

To run only small part, one line or like, you can use:

php -r '$x = "Hello World"; echo "$xn";'

If you are running linux then do man php at console.

if you need/want to run php through fpm, use cli fcgi

SCRIPT_NAME="file.php" SCRIP_FILENAME="file.php" REQUEST_METHOD="GET" cgi-fcgi -bind -connect "/var/run/php-fpm/php-fpm.sock"

where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.

Wednesday, March 31, 2021
 
nika
answered 7 Months ago
18

No, you cannot do that directly. You have a few options:

  • Put every function in a separate php file and call the php file
  • use the first argument passed to the php file as the function name, and write a few lines of code to select the correct function.

Update:

Here is a example of using the first passed parameter as a function call:

if(function_exists( $argv[1] ))
  call_user_func_array($argv[1], $argv);
Wednesday, March 31, 2021
 
buymypies
answered 7 Months ago
87

Using the -c option, you can specify which php.ini file should be used :

php -c /etc/php.ini your-php-script.php


As a reference, see the output of php --help :

$ php --help
Usage: php [options] [-f] <file> [--] [args...]
       php [options] -r <code> [--] [args...]
       php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
       php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
       php [options] -- [args...]
       php [options] -a

  -a               Run as interactive shell
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  ...
Thursday, August 5, 2021
 
Akdeniz
answered 3 Months ago
22

Your web server likely runs with other privileges than yourself. Possible problems include:

  • Path/file permission: can the web server user access the files it needs?
  • Different environment: are all necessary environment variables (PATH, Python-specific stuff, …) set?
  • Configuration: are there per-user configurations for Python or the module?

Tip: execute set in both the command prompt and from the PHP process and check the differences.

Saturday, August 14, 2021
 
adizone
answered 2 Months ago
47

Use phpunit --testdox
On the cli this will give you a very readable testdox format and allow you to see and fix your multiple test suites easily e.g.

PHPUnit 3.7.37 by Sebastian Bergmann.

Configuration read from /home/badass-project/tests/phpunit.xml

AnalyticsViewers
 [x] test getViewersForMonth throws for no valid date
 [x] test getViewersForMonth limits correctly
 [x] test getViewersForMonth only returns unprocessed records
 [ ] test getViewersForMonth marks retrieved records as processed
 [ ] test getViewersForMonth returns zero for no view data
 [x] test getViewersForMonth returns valid data

Organisation
 [x] test getOrganisation returns orgs

I use it in combination with the stack traces from a vanilla PHPUnit run to quickly setup.

It also has the added benefit of replacing underscores in your test function names with spaces. eg test_getViewersForMonth_returns_valid_data becomes test getViewersForMonth returns zero for no view data which is more human readable.
N.B. Generally speaking if you're following the PSR coding standards you should be using camelCase for method names but for unit tests methods I break this rule to reduce cognitive load during TDD development.

Sunday, September 5, 2021
 
aslum
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :