How to set $_GET and $_POST variables of php through python?

Viewed 368

I am working on a simple python web server for educational purposes. I want my server to be able to run PHP scripts on requests. I am able to execute php codes by creating a subprocess like in this answer and get its output as bytes. This can help me to execute the most of the PHP built-in functions. But I would like also to simulate the $_GET and $_POST variables, like, when the user send a POST request, I would like its parameters to be available in $_POST global variable.

I may hack the the command I give to the subprocess, but I heard that, in a real web server like Apache, PHP gets the value of these variables from the environment variables (Common Gateway Interface standard, I think).

However, when I set an environment variable called QUERY_STRING php doesn't set the value of $_GET. Let me show the big picture:

index.php

<?php var_dump($_GET); ?>

my commands in shell:

export QUERY_STRING="foo=bar"
php index.php

This command snipped doesn't give me the output I want (Array => ['foo'] = 'bar'). Here is my questions:

  • What is my mistake in the above code snipped ?
  • How can I inject these two global variables ($_GET and $_POST) to a PHP scripts in python?

My python version is 3.4.3 and I use ubuntu 14.04

1 Answers
Related