简体   繁体   English

将变量从 PHP 按钮传递到 python 脚本

[英]Passing variable from PHP button to python script

I've create PHP script that takes in user input and sends it to a Python script.我创建了 PHP 脚本,该脚本接受用户输入并将其发送到 Python 脚本。 The Python script creates an image which the PHP script displays. Python 脚本创建 PHP 脚本显示的图像。

Here's my Python code:这是我的 Python 代码:

import sys
import matplotlib.pyplot as plt
import numpy as np
import ftplib

result = sys.argv[1]

x = np.arange(0, result, 0.1)
y = np.sin(x)

plt.plot(x, y)
plt.savefig('image.png')

My PHP code:我的 PHP 代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Contact Form</title>
</head>
<body>
    <h2>Value identifier</h2>
    <p>Please fill in the value:</p>
    <form action="" method="post">
        <p>
            <label for="inputName">Name:</label>
            <input type="text" name="value" id="inputName">
        </p>
        <input type="submit" name="submit" value="Submit">
    </form>
</body>
</html>


<?php
    error_reporting(E_ALL); 
    ini_set('display_errors', 1);

    $values=$_POST['value'];
    $rad=exec("python test.py".$values);
    echo $values;
    echo $rad;
    echo "<img src='image.png'>";


?>

I don't get anything published, as if the Python script isn't even running.我没有发布任何内容,好像 Python 脚本甚至没有运行。 But printing the values I want to pass is successful.但是打印我想要传递的值是成功的。

The key line:关键线:

$rad=exec("python test.py".$values);
``
shouldn't be concatenated.  The period should be a comma.  Also, you don't need python before test.py.  You do need the filepath, however.  Also, you may want to try shell_exec() instead of exec() because that will just return a string, and you can print out the string to see what you are getting.  See below:

$rad = shell_exec('/home/path/path/path/RELATE/main.py') $rad = shell_exec('/home/path/path/path/RELATE/main.py')

Separately, this question is similar to the one you are asking and gives you a separate way to get PhP to talk to Python using a text file as an intermediary.  The benefit of this is that it would at least help you identify whether you have a PhP problem or a Python problem when there is a bug in your code.

https://stackoverflow.com/q/47981370/9807545

You are concatenating your arguments together without a space between them.您将 arguments 连接在一起,它们之间没有空格。 Further, you must always escape values before passing them to the shell.此外,在将值传递给 shell 之前,您必须始终对其进行转义。 Use escapeshellargs() for this .为此使用escapeshellargs()

<?php
if (isset($_POST["value"])) {
    $value = $_POST["value"];
    $rad = exec("python test.py ". escapeshellargs($value));
    echo $value;
    echo $rad;
    echo "<img src='image.png'>";
}
?>

You will also need to ensure that the user running the web server process has write permissions to the directory the scripts are contained in. This is unlikely to be the case, and certainly should not be the case.您还需要确保运行 web 服务器进程的用户对包含脚本的目录具有写入权限。这种情况不太可能出现,当然也不应该出现这种情况。 I'd suggest setting up a separate directory with appropriate permissions.我建议设置一个具有适当权限的单独目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM