Passing value from PHP script to Python script

Posted on

Question :

Passing value from PHP script to Python script

I looked at the other questions similar to this one, but can’t figure this out still.

I have a basic php file that does this:

$tmp = exec("python .$item");
echo $tmp;

While succesfully calls python that I have running on my webhostserver. Now in my python script i want something like this:

print item

Basically I’m asking how to pass variables from PHP to a python script and then back to php if necessary.


Answer #1:

Although netcoder pretty much gave you the answer in his comment, here’s an example:


import os
os.system("/usr/bin/php example2.php whatastorymark")


    echo $argv[1];



    $tmp = exec("python .$item");
    echo $tmp;


import sys
print sys.argv[1]

Here’s how PHP’s command line argument passing works:
The same for Python:

Answered By: Uku Loskit

Answer #2:

write a php file example index.php:

$sym = $_POST['symbols'];
echo shell_exec("python .$sym");

$sym is a parameter we passining to python file.
then create a python example

import sys

I hope it help you.

Answered By: Mani Kandan

Answer #3:

In php file

 $a = "..PythonPython39python.exe  ../frontend/upload/ '$param1''$param2'";

 $output = shell_exec($a);

 echo $output;


python path in my system, you can change with your path


it’s my python script path

In python file

import sys

param_1_ = sys.argv[1]
param_2=  sys.argv[2]
Answered By: Nikhil k gireesh

Leave a Reply

Your email address will not be published. Required fields are marked *