如何从PHP正确运行Python脚本


How to Correctly Run Python Script from PHP

我有一个python脚本,我想从PHP运行它。这是我的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之间交换数据,但上面的错误给出了输出:

错误空

我哪里错了?

:::::编辑::::::我运行了这个:

 $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

在我的机器上,代码运行良好,并显示:

array(1) {
  'status' =>
  string(4) "Yes!"
}

另一方面,您可以进行一些更改来诊断机器上的问题。

  1. 检查Python的默认版本。您可以通过从终端运行python来完成此操作。如果你看到类似的东西:

    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.
    >>>
    

    你很好。如果您看到您正在运行Python 3,这可能是一个问题,因为您的Python脚本是为Python 2编写的。因此:

    Python 3.4.0 (default, Apr 11 2014, 13:05:11)
    [...]
    

    应该是个线索。

  2. 再次从终端运行python myScript.py "['"as'",'"df'",'"gh'"]"。你看到了什么?

    {"status": "Yes!"}
    

    很酷。不同的响应表明问题可能与您的Python脚本有关。

  3. 检查权限。如何运行PHP脚本?您可以访问/path/to/吗?/path/to/myScript.php呢?

    将您的PHP代码替换为:

    <?php
    echo file_get_contents("/path/to/myScript.php");
    ?>
    

    你知道真正的内容吗?

  4. 现在,让我们在PHP代码中添加一些调试帮助程序。由于我认为您没有使用调试器,因此最简单的方法是打印调试语句。这对于10-LOC脚本来说是可以的,但如果您需要处理更大的应用程序,请投入时间学习如何使用PHP调试器以及如何使用日志记录。

    结果如下:

    /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)
    

    现在运行脚本:

    cd /path/to
    php -f demo.php
    

    这就是我得到的:

    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!"
    }
    

    你的应该有所不同,并包含关于正在发生的事情的提示。

我通过在参数周围添加引号来实现它!就像这样:<?php $data = array('as', 'df', 'gh'); $temp = json_encode($data); echo shell_exec('python myScript.py ' . "'" . $temp . "'"); ?>