【发布时间】:2015-04-21 18:01:30
【问题描述】:
我有一个想要从 PHP 运行的 python 脚本。这是我的 PHP 脚本:
$data = array('as', 'df', 'gh');
// Execute the python script with the JSON data
$result = shell_exec('python /path/to/myScript.py ' . escapeshellarg(json_encode($data)));
// Decode the result
$resultData = json_decode($result, true);
// This will contain: array('status' => 'Yes!')
var_dump($resultData);
这是我的 Python 脚本:
import sys, json
# Load the data that PHP sent us
try:
data = json.loads(sys.argv[1])
except:
print "ERROR"
sys.exit(1)
# Generate some data to send to PHP
result = {'status': 'Yes!'}
# Send it to stdout (to PHP)
print json.dumps(result)
我希望能够在 PHP 和 Python 之间交换数据,但是上面的错误给出了输出:
错误为空
我哪里错了?
:::::EDIT:::::: 我跑了这个:
$data = array('as', 'df', 'gh');
// Execute the python script with the JSON data
$temp = json_encode($data);
$result= shell_exec('C:\Python27\python.exe test.py ' . "'" . $temp . "'");
echo $result;
我收到No JSON object could be decoded
【问题讨论】:
-
如果你有这个解决方案可以分享一下
标签: php python linux shell command-line