Running PHP Scripts on CLI and Faking a Web Request

Some times you build a PHP script that is designed to run from a web request and you want to run it on the command line. The problem is that you have used the standard PHP super globals and you would rather not refactor the script loads.

Here is a nice quick solution:

    for ($i = 1; $i < $argc; $i++) {
    	$things = explode("=", $argv[$i]);
        if (false !== stripos($things[0], 'POST:')) {
             $post = str_replace('POST:', '', $things[0]);
             $_POST[$post] = $things[1];
        } else {
             $_GET[$things[0]] = $things[1];

echo "\nGET:\n";
echo "\nPOST:\n";

This will loop over the argc variable that is defined when running in CLI mode and will fake the $_GET and $_POST variables as required.

To run this with get and post variables you would do this:

php -f script.php getvar1=test getvar2=foo POST:postvar1=bar

And that’s it. It is not particularly pretty but it can be pretty handy.

Tags: phpwebcligetpost