views:

63

answers:

6

Hi all,
i have some scripts that need GET and POST values to start, and i wanna test them over shell.
Is there some way to pass the values to that arrays to avoid using getenv() function?

Thanks in advance!

+2  A: 

What you need is a wrapper script that sets the relevant globals and environment variables and then calls your script.

cletus
The best answer to my question was from `cletus`, and the best solution was from `umop`! +1 for both and Correct Answer for `umop`.Thank you all
CuSS
A: 

Take a look at: $_REQUEST

RobertPitt
`Note: When running on the command line , this will not include the argv and argc entries; these are present in the $_SERVER array . `thanks anyway...
CuSS
ii i missed that :( sorry
RobertPitt
A: 

In command line you call as

php test.php something1 something2 something3

and your test.php is

<?php
print_r($argv);
?>

and output is

Array
(
    [0] => test.php
    [1] => something1
    [2] => something2
    [3] => something3
)
marvin
+2  A: 

if your main goal is just to test from the command line, I would use the wget command and just call your script with the query string (for GET) and pass post data using the --post-data=string parameter of wget (for POST).

If your goal is to not use a webserver at all for testing for some reason, I'd recommend using a wrapper and encapsulating your access to GET and POST data so that you can test it either way.

umop
+1  A: 

Try something like this:

if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])){ // make sure we're running in CLI
    $args = $argv; // copy the argv array, we want to keep the original for (possible) future uses
    array_shift($args); // the $argv[0] is the filename, we don't need it
    for($i = 0;$i < ($argc - 1);$i++){
        list($key, $value) = explode('=', $args[$i]);
        $_REQUEST[$key] = $value;
    }
}

Of course, more features can be added by using getopts (like, --get abc=def ghi=jkl --post name=test passwd=test --cookie ilike=cookie) but that's up to you.

a110y
+1 for the code...didn't understood the last part of your answer.
CuSS
+1  A: 
if(php_sapi_name() == 'cli')
{
    associateGetPost();
}

function associateGetPost()
{
    $_GET = $_POST = array(); //Reset
    foreach($args as $id => $value)
    {
        if(substr($value,0,5) == '--get')
        {
            $_GET = parse_str(substr($value,5,-1))
        }elseif(substr($value,0,6) == '--post')
        {
            $_GET = parse_str(substr($value,6,-1))
        }
    }
}

Something along those lines.

RobertPitt
+1, but will use `umop` answer. Thanks for the Help `RobertPitt`
CuSS