SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing GET/POST variables to php via command line?

    How does one pass GET/POST variables via command line? For instance setting $getv=100 with a command line execution of test.php.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kadence View Post
    How does one pass GET/POST variables via command line? For instance setting $getv=100 with a command line execution of test.php.
    The command line doesn't have any concept of $_GET or $_POST, because these are purely http constructs

    Instead, values can be passed as command line arguments. I tend to use a format like:
    Code:
    php -f myScript.php -i=1 -j="value2"
    Then my script will contain somethin like:
    PHP Code:
    if ($_SERVER['argc'] > 1) {
        for (
    $commandLineIndex=1$commandLineIndex $_SERVER['argc']; $commandLineIndex++) {
            
    $commandLineArgument explode('='$_SERVER['argv'][$commandLineIndex]);
            
    $commandLineArgumentKey array_shift($commandLineArgument);
            
    $commandLineArgumentValue implode('='$commandLineArgument);

            switch(
    $commandLineArgumentKey) {
                case 
    '-i' :
                        if ((
    is_null($commandLineArgumentValue)) || (trim($commandLineArgumentValue) == '')) {
                            
    errorTerminate('A value must be specified for : '.$_SERVER['argv'][$commandLineIndex].PHP_EOL);
                        }
                        
    $i $commandLineArgumentValue;
                        break;
                case 
    '-j' :
                        if ((
    is_null($commandLineArgumentValue)) || (trim($commandLineArgumentValue) == '')) {
                            
    errorTerminate('A value must be specified for : '.$_SERVER['argv'][$commandLineIndex].PHP_EOL);
                        }
                        
    $j $commandLineArgumentValue;
                        break;
                default :
                        
    errorTerminate('Unknown argument : '.$_SERVER['argv'][$commandLineIndex].NEWLINE.'Use -h or --help for help');
                        break;
            }
        }

    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    New Jersey
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. So there's no workaround to pass REQUEST variables via command line? Because the script this is for is a very complex pre-written script, which there's no chance of me being able to recode to be used with the traditional CLI.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You will have to manually take the command line arguments and inject them into _REQUEST, _GET, or _POST. Or just rewrite the script anyways...
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From the command line (on Linux) you can run
    Code:
    wget http://domain.path/to/script.php
    unfortunately with this method your cli page will be acessible from the Internet same as any other page, the good thing is you can send $_REQUEST variables.

  6. #6
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wonder where's the problem at all
    Web-server usually provides POST/GET data to the script, so, you have no worry about it.
    How do you call yor script exactly? What aplication calls it?

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    Web-server usually provides POST/GET data to the script, so, you have no worry about it.
    How do you call yor script exactly? What aplication calls it?
    There is no web server involved in this at all.
    The OP is calling the script from the command line
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  8. #8
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If there is no web server, there is no GET/POST.
    What is OP?
    In which form this OP has his POST data? a text file? a database record?

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $ setenv GATEWAY_INTERFACE CGI/1.1
    $ setenv REQUEST_METHOD GET
    $ setenv QUERY_STRING v=100
    $ setenv PATH_TRANSLATED "file.php"
    $ /path/to/php-cgi file.php
    http://hoohoo.ncsa.illinois.edu/cgi/interface.html


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •