5

I'm currently using subprocess.call(["php", "test.php"]) in my python script to call a PHP script.

I would like to know if it's possible to pass $_GET parameters to the script while calling it. If it's possible how would I do it?

This is what I'm trying to get:
"subprocess.call(["php", "test.php?i=3"])"


This is my current code:
test.py

import subprocess
subprocess.call(["php", "test.php"])


test.php

<?php
    echo $_GET['i'];
?>
Matthew
  • 146
  • 2
  • 9

2 Answers2

3

Try the following with your Python passing in as many params as you'd like.

subprocess.call(["php","-f","test.php","param0:value0", "param1:value1"])

Now in PHP:

<?php
$_GET = array();

foreach($argv as $key => $pair) {
    if ($key == 0) { //skip first element which is script name (test.php)
        continue;
    }

    list($key, $value) = explode(":", $pair);
    $_GET[$key] = $value;
}

This manually parses $argv and populates $_GET for you so you only have modify the beginning of your PHP file.

This is an imperfect solution if your key or value have the same value as the : delimiter. An even better way would be to pass in the parameters via a JSON string. Something like:

subprocess.call(["php","-f","test.php", json.dumps(your_dictionary)])

and in PHP

<?php
if ($argc > 1) {
  $json_str = $argv[1];
  $_GET = json_decode($json_str);
}

and finally as suggested by user574632, you can wrap this code with the following if statment to ensure it doesn't interfere if you're still running it as a web-script as well.

if (php_sapi_name() == 'cli') {
  //execute in here *only* if run from the command-line
}
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • Populating GET manually suggests the script already works with get vars, and is therefor perhaps used from command and web, in which case a conditional check for `PHP_SAPI == 'cli'` might be in order – Steve May 28 '14 at 00:14
  • So I tried `subprocess.call(["php", "-f", "test.php", "1234", "3", "s"])`As for my php script it looks like this: `echo var_dump($argv)` and I'm getting `NULL` I even tried using a foreach loop to print all the values and I'm not getting anything back – Matthew May 28 '14 at 00:42
  • Take this one step at a time - first call `php -f test.php 1234 3 s` manually from the command-line. – Martin Konecny May 28 '14 at 00:45
  • So I called `php -f test.php 1234 3 s` in the command line alone, I got the same results: `NULL` with a `count()` of `0` [php code](http://puu.sh/949w5.png) [results](http://puu.sh/949zT.png) – Matthew May 28 '14 at 00:49
  • Post the beginning of the file to the part where you `var_dump` – Martin Konecny May 28 '14 at 00:52
  • Here's the official documentation: http://www.php.net/manual/en/reserved.variables.argv.php As you can see it's quite simple. Try create a new test file with nothing but ` – Martin Konecny May 28 '14 at 01:00
  • New php: ` – Matthew May 28 '14 at 01:04
  • Your problem seems to have something to do with setting `register_argc_argv`. Take a look here: http://stackoverflow.com/questions/11459959/php-how-to-override-register-argc-argv - you can follow that advice or just edit your php.ini file, and set `register_argc_argv = On` – Martin Konecny May 28 '14 at 02:23
2

You wouldn't call it like that, more like this:

subprocess.call(["php","-f","test.php","1"])

Where 1 is the value of the argument. Then, in your script:

<?php
echo $argv[0];
?>

This gives you the value. My python is a bit rusty, but that's the basics.

Nathan C
  • 180
  • 11