Use a GET-like method to pass Parameters in PHPCLI

Source: Internet
Author: User
Tags php cli
In Linux, for convenience, sometimes php programs are directly executed using the command line method, such as some crontab tasks, but most php programs are usually written, it is normal to directly use get to pass Parameters without considering some situations in the command line, but this is helpless in the command line, especially when multiple GET parameters are passed, not even commands

In Linux, for convenience, sometimes php programs are directly executed using the command line method, such as some crontab tasks, but most php programs are usually written, it is normal to directly use get to pass Parameters without considering some situations in the command line, but this is helpless in the command line, especially when multiple GET parameters are passed, not even commands

In Linux, for convenience, sometimes php programs are directly executed using the command line method, such as some crontab tasks, but most php programs are usually written, it is normal to directly use get to pass Parameters without considering some situations in the command line, but this is helpless in the command line, especially when multiple GET parameters are passed, even commands cannot be correctly executed -- GET multiple parameter delimiters will impede command execution.

1. php cli parameter passing Method

In php cli, parameters are passed through the Linux Command Line. The parameter values are arranged in sequence according to the specified sequence. If the sequence is incorrect, the final result is also incorrect, to support command line parameters, you also need to analyze and split the $ argv array. If you modify an existing program, the workload will increase.

2. Capture parameters and assemble the pseudo $ _ GET and $ _ REQUEST arrays.

There are no variables such as $ _ GET in the command line, but if you use GET for parameter passing, there will certainly be calls to arrays such as $ _ GET and $ _ REQUEST in the program. To reduce the modification workload, the simplest way is to capture the parameters passed by the CLI, And Then assemble the parameters into a $ _ GET and $ _ REQUEST arrays.
To avoid the Parameter order requirements in cli and make it easier for command execution to identify more familiar URL parameter passing methods, you can pass parameters and capture them in the following format:

1

php filename.php "name=Roges&height=187.96&weight=99.79"

Note: Use&It must be enclosed in quotation marks. Otherwise, you will be pleasantly surprised.

3. Implementation

After thinking about the process, it is relatively simple to implement. A simple implementation is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// if file was executed by php cli, cli paramertes will be convert

// to $_GET and $_REQUEST array.

if(isset($argv) AND count($argv) <= 2) {

// GET paramertes are always follow the page url with ? symtax,

// it show be removed.

$argv[1] = str_replace("?", '', $argv[1]);

$params = explode('&', $argv[1]);

$_REQUEST = array();

foreach($params as $p) {

//get the location of the '='

$eq_loc = strpos($p, '=');

$_REQUEST[substr($p, 0, $eq_loc)] = substr($p, $eq_loc + 1, strlen($p) - $eq_loc);

}

$_GET = $_REQUEST;

}

Store the code as a file and then reference it in the file header. When the script is executed in the command line, the conversion is performed. Otherwise, no operation is performed.

4. execution results

Save the above Code and add the following two sentences at the end to view the effect of parameter passing:

1

2

3

4

echo '$_REQUEST: ';

print_r($_REQUEST);

echo '$_GET: ';

print_r($_GET);

Run the file command cli_get.php in the command line and the result is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

Roges@US:~/www$ php cli_get.php "name=Roges&height=187.96&weight=99.79"

$_REQUEST: Array

(

[name] => Roges

[height] => 187.96

[weight] => 99.79

)

$_GET: Array

(

[name] => Roges

[height] => 187.96

[weight] => 99.79

)

Choose a cell phone or Blackberry. The big screen is just a toy.

In Linux, for convenience, sometimes php programs are directly executed using the command line method, such as some crontab tasks, but most php programs are usually written, it is normal to directly use get to pass Parameters without considering some situations in the command line, but this is helpless in the command line, especially when multiple GET parameters are passed, even commands cannot be correctly executed -- GET multiple parameter delimiters will impede command execution.

1. php cli parameter passing Method

In php cli, parameters are passed through the Linux Command Line. The parameter values are arranged in sequence according to the specified sequence. If the sequence is incorrect, the final result is also incorrect, to support command line parameters, you also need to analyze and split the $ argv array. If you modify an existing program, the workload will increase.

2. Capture parameters and assemble the pseudo $ _ GET and $ _ REQUEST arrays.

There are no variables such as $ _ GET in the command line, but if you use GET for parameter passing, there will certainly be calls to arrays such as $ _ GET and $ _ REQUEST in the program. To reduce the modification workload, the simplest way is to capture the parameters passed by the CLI, And Then assemble the parameters into a $ _ GET and $ _ REQUEST arrays.
To avoid the Parameter order requirements in cli and make it easier for command execution to identify more familiar URL parameter passing methods, you can pass parameters and capture them in the following format:

1

php filename.php "name=Roges&height=187.96&weight=99.79"

Original article address: Use a GET-like method to pass Parameters in php cli. Thank you for sharing it with the original author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.