Skip to content

Commit aa35d14

Browse files
committed
feat: add support for passing in a request method or URL to simulate
1 parent e9025f0 commit aa35d14

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# php-fpm-cli
22

3-
Running PHP-FPM, especially for website hosts using NGINX, is missing an important functionality to execute PHP scripts directly from the command-line.
3+
Running PHP-FPM, especially for website hosts using NGINX, is missing the important ability to execute PHP scripts directly from the command-line while simulating the website context.
44

5-
With this simple script, one can execute PHP scripts directly from CLI. It is convenient to test PHP in CLI, run batch operations with PHP and for cronjobs.
5+
With this simple script, one can execute PHP scripts that are normally called via PHP-FPM, directly from CLI. This can be used for testing, batch operations, or cronjobs.
66

7-
This script is based on [Mathias Leppich's gist][1], with added support for loading the script from a file (instead of input code as CLI argument), query-string parameters and verification of not only the exit code of the script - but also http-status-code.
7+
This script is based on [Mathias Leppich's gist][1], with added support for:
8+
* loading the script from a file (instead of input code as CLI argument)
9+
* query-string parameters
10+
* verification of not only the exit code of the script, but also http-status-code.
11+
* guessing of `$_SERVER` variables given a URL to simulate
812

9-
See more details in: https://elisegev.medium.com/running-php-fpm-in-cli-e1f0f4b4f59a
13+
See more details in [this Medium post](https://elisegev.medium.com/running-php-fpm-in-cli-e1f0f4b4f59a).
1014

1115
## Requirements
1216

@@ -24,7 +28,7 @@ apt-get install libfcgi0ldbl
2428

2529
### Setup
2630

27-
PHP script needs to be executable and owned by an allowed user (ACL):
31+
The PHP script being executed needs to be executable and owned by an user allowed by the PHP-FPM configuration, so you may need to update it:
2832
```shell script
2933
vi /etc/php-fpm.d/www.conf
3034
# add your ACL user to the comma-delimited list: listen.acl_users
@@ -34,19 +38,29 @@ systemctl restart php-fpm
3438

3539
## Usage
3640

37-
Run PHP script:
41+
The script will send the given PHP file to the PHP-FPM process and echo the response to STDOUT.
42+
3843
```shell script
39-
./php-fqm-cli -f <path_to_php_file>
44+
./php-fpm-cli -f </path/to/php/file> -q "<query_params>" [-connect <connection>] [-m <GET|POST>] [-u <URL>]
4045
```
4146

42-
Run PHP script with query params:
47+
Parameters:
48+
| Flag | Description | Default | Example |
49+
| ---- | ----------- | ------- | ------- |
50+
| `-f` | Path to the PHP file | | `/var/www/html/index.php` |
51+
| `-connect` | PHP-FPM host and port, or path to a socket | `/run/php-fpm/www.sock` | `127.0.0.1:9000` |
52+
| `-m` | Request method, GET or POST | GET | |
53+
| `-u` | URL to simulate. This must return an status code 200, so make sure this is the canonical URL (i.e., it will not redirect) | | `https://www.mydomain.com/` |
54+
55+
56+
Using query parameters:
4357
```shell script
44-
./php-fqm-cli -f <path_to_php_file> -q "<query_params>"
58+
./php-fpm-cli -f myscript.php -q "param1=val1&param2=val2"
4559
```
4660

47-
Example:
61+
Using a URL:
4862
```shell script
49-
./php-fqm-cli -f myscript.php -q "param1=val1&param2=val2"
63+
./php-fpm-cli -f /var/www/html/index.php -connect 127.0.0.1:9000 -u https://www.mydomain.com/
5064
```
5165

5266
[1]: https://gist.github.com/muhqu/91497df3a110f594b992

php-fpm-cli

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,67 @@ Options:
3232
3333
-f <code> Run PHP file without using script tags
3434
35-
-q <code> query string for running the php file
35+
-q <code> Query string for running the php file
36+
37+
-m <GET|POST> request method
38+
Default: GET
39+
40+
-u <url> Full URL to simulate; this will try to set $_SERVER variables appropriately.
41+
Only PHP files directly under the document root are currently supported.
42+
You may need to include "/index.php" since we don't have use of Apache
43+
RewriteRules.
44+
example: https://www.mydomain.com/index.php
3645
3746
USAGE
3847
}
3948

4049
main() {
4150
res=0
4251

52+
if [ -n $PHP_URL ]
53+
then
54+
# URL parsing code from https://stackoverflow.com/a/6174447/1282424
55+
56+
# extract the protocol
57+
proto="$(echo $PHP_URL | grep :// | sed -e's,^\(.*://\).*,\1,g')"
58+
59+
if [[ $proto =~ https ]]
60+
then
61+
https="on"
62+
fi;
63+
64+
# remove the protocol -- updated
65+
url_no_proto=$(echo $PHP_URL | sed -e s,$proto,,g)
66+
67+
# extract the user (if any)
68+
user="$(echo $url_no_proto | grep @ | cut -d@ -f1)"
69+
70+
# extract the host and port -- updated
71+
hostport=$(echo $url_no_proto | sed -e s,$user@,,g | cut -d/ -f1)
72+
73+
# by request host without port
74+
host="$(echo $hostport | sed -e 's,:.*,,g')"
75+
76+
# by request - try to extract the port
77+
port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
78+
79+
# extract the path (if any)
80+
path="$(echo $url_no_proto | grep / | cut -d/ -f2-)"
81+
82+
# Assume the document root is the directory containing $PHP_FILE_PATH
83+
document_root=`dirname $PHP_FILE_PATH`
84+
fi;
85+
4386
#Execute PHP
87+
SCRIPT_NAME=/$path \
88+
DOCUMENT_ROOT=$document_root \
89+
HTTP_HOST=$host \
90+
SERVER_NAME=$host \
91+
REQUEST_URI=/$path \
4492
SCRIPT_FILENAME=$PHP_FILE_PATH \
93+
HTTPS=$https \
4594
QUERY_STRING=$PHP_QUERY_STRING \
46-
REQUEST_METHOD=GET \
95+
REQUEST_METHOD=$PHP_REQUEST_METHOD \
4796
cgi-fcgi -bind -connect "$CONN" > $TMP_OUTPUT_FILE_PATH #without dumping to file, cgi-fcgi hides "Status"
4897

4998
#Get successful-execution indications
@@ -68,6 +117,8 @@ main() {
68117
CONN="/run/php-fpm/www.sock"
69118
PHP_FILE_PATH=""
70119
PHP_QUERY_STRING=""
120+
PHP_URL=""
121+
PHP_REQUEST_METHOD="GET"
71122
TMP_OUTPUT_FILE_PATH="/tmp/php_fpm_cli_output.tmp"
72123
init() {
73124
until [ -z "$1" ]; do
@@ -78,6 +129,10 @@ init() {
78129

79130
-q) shift; PHP_QUERY_STRING="$1"; shift; ;;
80131

132+
-m) shift; PHP_REQUEST_METHOD="$1"; shift; ;;
133+
134+
-u) shift; PHP_URL="$1"; shift; ;;
135+
81136
help|-h|-help|--help)
82137
usage;
83138
exit 0

0 commit comments

Comments
 (0)