23

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:


?php
$item='example';
$tmp = exec("python testscriptphp.py .$item");
echo $tmp;
?

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


item=$item
print item

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

Thanks!

1
  • 6
    Have you heard of sys.argv (Python) and $argv (PHP) ? :) Commented Feb 12, 2011 at 8:41

3 Answers 3

31

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

Python->PHP

example.py

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

example2.php

<?php
    echo $argv[1];

?>

PHP->Python

<?php
    $item='example';
    $tmp = exec("python testscriptphp.py .$item");
    echo $tmp;

?>

testscriptphp.py

import sys
print sys.argv[1]

Here's how PHP's command line argument passing works: http://php.net/manual/en/reserved.variables.argv.php The same for Python: http://docs.python.org/library/sys.html#sys.argv

Sign up to request clarification or add additional context in comments.

2 Comments

is it possible to update the variable rather than echo? from python to php?
@ravi I think, $argv[1] = 1234;
4

write a php file example index.php:

<?PHP
$sym = $_POST['symbols'];
echo shell_exec("python test.py .$sym");
?>

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

import sys
print(sys.argv[1])

I hope it help you.

Comments

-1

In php file

 $a = "..\Python\Python39\python.exe  ../frontend/upload/main.py '$param1''$param2'";

 $output = shell_exec($a);

 echo $output;

 ..\Python\Python39\python.exe 

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

 ../frontend/upload/main.py      

it's my python script path

In python file

import sys

param_1_ = sys.argv[1]
param_2=  sys.argv[2]

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.