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!
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!
What you need is a wrapper script that sets the relevant globals and environment variables and then calls your script.
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
)
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.
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.
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.