calling exec on a php file and passing parameters?

前端 未结 5 1700
慢半拍i
慢半拍i 2021-02-19 00:53

I am wanting to call a php file using exec.

When I call it I want to be able to pass a variable through (an id).

I can call echo exec(\"php /var/www/unity/

相关标签:
5条回答
  • 2021-02-19 00:55

    If you want to pass a GET parameter to it, then it's mandatory to provide a php-cgi binary for invocation:

    exec("QUERY_STRING=id=123 php-cgi /var/www/emailer.php");
    

    But this might require more fake CGI environment variables. Hencewhy it is often advisable to rewrite the called script and let it take normal commandline arguments and read them via $_SERVER["argv"].

    (You could likewise just fake the php-cgi behaviour with a normal php interpreter and above example by adding parse_str($_SERVER["QUERY_STRING"], $_GET); on top of your script.)

    0 讨论(0)
  • 2021-02-19 00:55

    I know this is an old thread but it helped me solve a problem so I want to offer an expanded solution. I have a php program that is normally called through the web interface and takes a long list of parameters. I wanted to run it in the background with a cron job using shell_exec() and pass a long list of parameters to it. The parameter values change on each run.

    Here is my solution: In the calling program I pass a string of parameters that look just like the string a web call would send after the ?. example: sky=blue&roses=red&sun=bright etc. In the called program I check for the existence of $argv[1] and if found I parse the string into the $_GET array. From that point forward the program reads in the parameters just as if they were passed from a web call.

    Calling program code:

    $pars = escapeshellarg($pars); // must use escapeshellarg()
    $output = shell_exec($php_path . ' path/called_program.php ' . $pars); // $pars is the parameter string
    

    Called program code inserted before the $_GET parameters are read:

    if(isset($argv[1])){ // if being called from the shell build a $_GET array from the string passed as $argv[1]
        $args = explode('&', $argv[1]); // explode the string into an array of Type=Value elements
        foreach($args as $arg){
            $TV = explode('=', $arg); // now explode each Type and Value into a 2 element array
            $_GET[$TV[0]] = $TV[1]; // set the indexes in the $_GET array
            }
        }
    //------------------------
    // from this point on the program processes the $_GET array normally just as if it were passed from a web call.
    

    Works great and requires minimal changes to the called program. Hope someone finds it of value.

    0 讨论(0)
  • 2021-02-19 00:59

    Your call is failing because you're using a web-style syntax (?parameter=value) with a command-line invokation. I understand what you're thinking, but it simply doesn't work.

    You'll want to use $argv instead. See the PHP manual.

    To see this in action, write this one-liner to a file:

    <?php print_r($argv); ?>
    

    Then invoke it from the command-line with arguments:

    php -f /path/to/the/file.php firstparam secondparam
    

    You'll see that $argv contains the name of the script itself as element zero, followed by whatever other parameters you passed in.

    0 讨论(0)
  • 2021-02-19 01:19

    this adapted script shows 2 ways of passing parameters to a php script from a php exec command: CALLING SCRIPT

    <?php 
    $fileName = '/var/www/ztest/helloworld.php 12';
    $options = 'target=13';
    exec ("/usr/bin/php -f {$fileName} {$options} > /var/www/ztest/log01.txt 2>&1 &");
    
    echo "ended the calling script"; 
    ?>
    

    CALLED SCRIPT

    <?php
    echo "argv params: ";
    print_r($argv); 
    if ($argv[1]) {echo "got the size right, wilbur!  argv element 1: ".$argv[1];}
    ?> 
    

    dont forget to verify execution permissions and to create a log01.txt file with write permissions (your apache user will usually be www-data).

    RESULT

    argv params: Array

    (

    [0] => /var/www/ztest/helloworld.php
    
    [1] => 12
    
    [2] => target=13
    

    )

    got the size right, wilburargv element 1: 12

    choose whatever solution you prefer for passing your parameters, all you need to do is access the argv array and retrieve them in the order that they are passed (file name is the 0 element).

    tks @hakre

    0 讨论(0)
  • 2021-02-19 01:22

    try echo exec("php /var/www/unity/src/emailer.php 123"); in your script then read in the commandline parameters.

    0 讨论(0)
提交回复
热议问题