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'];
?>
2
  • are you serving this php scripts under apache or fpm? why not use requests docs.python-requests.org/en/latest Commented May 27, 2014 at 23:55
  • 2
    No you won't be able to do this. You need to edit the php script and get it to parse $argv Commented May 27, 2014 at 23:56

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

8 Comments

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
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
Take this one step at a time - first call php -f test.php 1234 3 s manually from the command-line.
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 results
Post the beginning of the file to the part where you var_dump
|
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.

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.