0

I am debugging a php script that will be called by a javascript function and that have POST arguments. I would like to test it in command line mode. I know it is possible doing it with the php -a option. But once in the interactive mode how to I set up the arguments? And how do I call my php? I can't neither remenber nor find how to do it.

My php script is:

<?php
$data = $_POST['string'];
$fname = $_POST['file'];
$dir=$_POST['dir'];
mkdir($dir);
$file = fopen($fname, 'w');
fwrite($file, $dir."\n");
fwrite($file, $data."\n");
fwrite($file, "/var/www/html/ChemAlive_app/SOFTWARE/utilities/"."\n");
fclose($file);
$saved = getenv("LD_LIBRARY_PATH");        // save old value
$newld = "/usr/local/NWChem/lib/";  // extra paths to add
if ($saved) { $newld .= ":$saved"; }           // append old paths if any
putenv("LD_LIBRARY_PATH=$newld");
$saved = getenv("PATH");        // save old value
$newld = "/usr/local/NWChem/bin/";  // extra paths to add
if ($saved) { $newld .= ":$saved"; }           // append old paths if any
putenv("PATH=$newld");
exec("cd $dir ;  /var/www/html/ChemAlive_app/SOFTWARE/ChemAliveExec/ReactionThermo ".$fname);
?> 

Thanks for your help.

4
  • 2
    If you are happy to use a separate console command, use curl. You can do it with PHP as well, but cURL via the console is more succinct. Commented Mar 20, 2015 at 8:58
  • I concur, trying to reproduce a web server environment is much too complicated when you can simply reproduce the HTTP request instead. Commented Mar 20, 2015 at 9:09
  • that seems promising, how does that works? Commented Mar 20, 2015 at 9:16
  • Browsers aren't the only things that can produce HTTP requests. curl is a command line utility that can do the same. There are myriads of tools available for this. Google curl and/or "REST API test tool" or such. Commented Mar 20, 2015 at 10:17

1 Answer 1

0

you can use the following:

<?php
// check to see if php called from cmd line 
if (php_sapi_name() == 'cli') {
       //take vars from cmd line args argv[0] is script name
       $data = $argv[1];
       $fname = $argv[2];
       $dir = $argv[3];
} else {
       $data = $_POST['string'];
       $fname = $_POST['file'];
       $dir=$_POST['dir'];
}

then call the script on the command line with

/path/to/php /path/to/script.php 'some data' 'filename' 'dirname'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.