将值从PHP脚本传递到Python脚本

时间:2022-02-15 17:18:18

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文件,它执行此操作:


?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:

虽然成功调用了我在webhostserver上运行的python。现在在我的python脚本中我想要这样的东西:


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.

基本上我问的是如何将变量从PHP传递到python脚本,然后在必要时返回php。

Thanks!

1 个解决方案

#1


24  

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

尽管netcoder在他的评论中给出了答案,但这里有一个例子:

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

以下是PHP的命令行参数传递的工作方式:http://php.net/manual/en/reserved.variables.argv.php Python的相同内容:http://docs.python.org/library/sys.html#sys .argv

#1


24  

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

尽管netcoder在他的评论中给出了答案,但这里有一个例子:

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

以下是PHP的命令行参数传递的工作方式:http://php.net/manual/en/reserved.variables.argv.php Python的相同内容:http://docs.python.org/library/sys.html#sys .argv