问题描述
我有一个python脚本,我想从PHP运行。这是我的PHP脚本:
$ data = array('as','df','gh');
//使用JSON数据执行python脚本
$ result = shell_exec('python /path/to/myScript.py'。escapeshellarg(json_encode($ data))));
//解码结果
$ resultData = json_decode($ result,true);
//这将包含:array('status'=>'Yes!')
var_dump($ resultData);
这是我的Python脚本:
import sys,json
pre>
#加载PHP发送给我们的数据
try:
data = json.loads(sys.argv [ 1])$ b $ b except:
printERROR
sys.exit(1)
#生成一些数据发送到PHP
result = { 'status':'Yes!'}
#发送到stdout(到PHP)
print json.dumps(result)
我想在PHP和Python之间交换数据,但上面的错误给出了输出:
我错了什么?
::::: EDIT ::::::
我运行了:$ data = array('as','df','gh');
//使用JSON数据执行python脚本
$ temp = json_encode($ data);
$ result = shell_exec('C:\Python27\python.exe test.py'。'。$ temp。');
echo $ result;
我得到
没有JSON对象可以解码
解决方案在我的机器上,代码工作得很好,并显示:
array(1){
'status'=>
string(4)是!
}
另一方面,您可以进行一些更改以诊断问题您的机器。
检查Python的默认版本。你可以通过从终端运行
python
来做到这一点。如果你看到像:Python 2.7.6(默认,2014年3月22日,22: 59:56)
[GCC 4.8.2] on linux2
更多信息,请输入help,copyright,credits或license。
>>>>
如果你看到你正在运行Python 3,这可能是一个问题,因为你的Python脚本是为Python 2编写的。因此:
Python 3.4.0(默认,2014年4月11日,13:05:11)
[...]
再次从终端,运行
python myScript.py[\as \,\df \,\gh \]
。你看到了什么?{status:Yes!}
很酷。不同的回应表示问题可能来自您的Python指令码。
检查权限。如何运行PHP脚本?您是否可以访问
/ path / to /
?/path/to/myScript.php
?
以下列方式替换您的PHP程式码:
<?php
echo file_get_contents(/ path / to / myScript.php);
?>
您是否获得实际内容?
现在让我们在PHP代码中添加一些调试助手。因为我想你没有使用调试器,最简单的方法是打印调试语句。这适用于10-LOC脚本,但如果您需要处理较大的应用程序,请投入时间学习如何使用以及如何使用。
以下是结果:
/path/to/demo.php
<?php
$ data = array('as','df','gh');
$ pythonScript =/path/to/myScript.py;
$ cmd = array(python,$ pythonScript,escapeshellarg(json_encode($ data))));
$ cmdText = implode('',$ cmd);
echo运行命令:。 $ cmdText。 \\\
;
$ result = shell_exec($ cmdText);
echo得到以下结果:\\\
;
echo $ result;
$ resultData = json_decode($ result,true);
echo结果转换为:\\\
;
var_dump($ resultData);
?>
/path/to/myScript.py
$ bimport sys,json
try:
data = json.loads .argv [1])$ b $ b print json.dumps({'status':'Yes!')
except Exception as e:
print str(e)
现在运行脚本:
cd / path / to
php -f demo.php
get:
运行命令:python /path/to/myScript.py' ,df,gh]'
得到以下结果:
{status:Yes!}
结果转换为:
array ){
'status'=>
string(4)是!
}
yours应该是不同的,并包含发生的情况。 >
I have a python script that I would like to run from PHP. This is my PHP script:
$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);
And this is my Python script:
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)
I would like to be able to exchange data between PHP and Python, but the above error gives the output:
Where am I going wrong ?
:::::EDIT::::::I ran this:
$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;
I am getting
No JSON object could be decoded
解决方案
On my machine, the code works perfectly fine and displays:
array(1) {
'status' =>
string(4) "Yes!"
}
On the other hand, you may make a few changes to diagnose the issue on your machine.
Check the default version of Python. You can do this by running
python
from the terminal. If you see something like:Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
you're fine. If you see that you are running Python 3, this could be an issue, since your Python script is written for Python 2. So:
Python 3.4.0 (default, Apr 11 2014, 13:05:11) [...]
should be a clue.
Again from the terminal, run
python myScript.py "[\"as\",\"df\",\"gh\"]"
. What do you see?{"status": "Yes!"}
is cool. A different response indicates that the issue is probably with your Python script.
Check permissions. How do you run your PHP script? Do you have access to
/path/to/
? What about/path/to/myScript.php
?Replace your PHP code by:
<?php echo file_get_contents("/path/to/myScript.php"); ?>
Do you get the actual contents?
Now let's add a few debugging helpers in your PHP code. Since I imagine that you are not using a debugger, the simplest way is to print debug statements. This is OK for 10-LOC scripts, but if you need to deal with larger applications, invest your time in learning how to use PHP debuggers and how do use logging.
Here's the result:
/path/to/demo.php
<?php $data = array('as', 'df', 'gh'); $pythonScript = "/path/to/myScript.py"; $cmd = array("python", $pythonScript, escapeshellarg(json_encode($data))); $cmdText = implode(' ', $cmd); echo "Running command: " . $cmdText . "\n"; $result = shell_exec($cmdText); echo "Got the following result:\n"; echo $result; $resultData = json_decode($result, true); echo "The result was transformed into:\n"; var_dump($resultData); ?>
/path/to/myScript.py
import sys, json try: data = json.loads(sys.argv[1]) print json.dumps({'status': 'Yes!'}) except Exception as e: print str(e)
Now run the script:
cd /path/to php -f demo.php
This is what I get:
Running command: python /path/to/myScript.py '["as","df","gh"]' Got the following result: {"status": "Yes!"} The result was transformed into: array(1) { 'status' => string(4) "Yes!" }
yours should be different and contain a hint about what is happening.
这篇关于如何正确地从PHP运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!