从 PHP 运行 Python 程序
phpserver side programmingprogrammingpython更新于 2025/9/27 13:00:17
在 PHP 中,可以使用 ‘shell_exec’ 函数。它可以通过 shell 执行,结果可以作为字符串返回。如果从命令行传递了 NULL 或根本没有返回输出,它将返回错误。
下面是相同的代码演示 −
<?php
$command_exec = escapeshellcmd('path-to-.py-file');
$str_output = shell_exec($command_exec);
echo $str_output;
?>
需要授予正确的权限,以便成功执行 Python 脚本。
注意 − 在 Unix 类型的平台上工作时,PHP 代码以 Web 用户身份执行。因此,应授予 Web 用户对目录和子目录的必要权限。
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

