【问题标题】:How to Correctly Run Python Script from PHP [duplicate]如何从 PHP 正确运行 Python 脚本 [重复]
【发布时间】: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


【解决方案1】:

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

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 debuggers 以及如何使用 logging

    结果如下:

    /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 运行它
  • @harvey_slash:我不确定您的评论与我的回答是否相关。
  • 如何从 php 运行 python myScript.py "[\"as\",\"df\",\"gh\"]" ?
  • @harvey_slash:对不起,我的回答不清楚。我重新制定了它。
  • 从 php 运行它,你的方式,它工作。但是如何让它从 json_encode() 工作?
【解决方案2】:

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

【讨论】:

  • 似乎对我不起作用:/
  • 如果您尝试将您的 data 变量打印回 PHP 而不是您的 result 变量,这是否会产生任何结果?
  • var_dump 给出:array(3) { [0]=> string(2) "as" [1]=> string(2) "df" [2]=> string(2) “gh”}
猜你喜欢
  • 2017-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
  • 2012-10-01
  • 1970-01-01
  • 2018-01-08
  • 1970-01-01
相关资源
最近更新 更多