0

有一个用 PHP 和 HTML 编写的 Web 应用程序。我想要的是针对各种情况过滤用户输入并对其进行清理。例如,我想将来自表单(字符串)的输入与允许的字符串列表进行比较,并根据触发合适的 PHP 函数来处理它是对还是错。

我的问题是如何将用户输入与 python 脚本绑定,然后将这个 python 脚本的结果作为 PHP 的输入?

谢谢

4

1 回答 1

0

您可以将 PHP 文件中的 Python 脚本作为 shell 命令调用,并向其传递 JSON 格式的参数。然后让 Python 脚本输出响应(也是 JSON 编码的)并让 PHP 文件捕获它。这是我最近使用的一个示例,由以下链接拼凑而成:

PHP 文件:

$py_input = ... // Your data goes here.
// Call the Python script, passing it the JSON argument, and capturing the result.
$py_output = shell_exec('python script.py ' . escapeshellarg(json_encode($py_input)));
$py_result = json_decode($py_output);

蟒蛇文件:

import json

php_input = json.loads(sys.argv[1])  # The first command line argument.
# Do your thing.
php_output = ... # Whatever your output is.
print json.dumps(php_output) # Print it out in JSON format.

将 Python 列表传递给 php

在 PHP 中执行 Python 脚本并在两者之间交换数据

于 2014-08-05T18:32:27.827 回答