Passing value from PHP script to Python script

stackVidec picture stackVidec · Feb 12, 2011 · Viewed 35.9k times · Source

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!

Answer

Uku Loskit picture Uku Loskit · Feb 12, 2011

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