技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

如何在 PHP 中调用 Python 文件?

pythonserver side programmingprogrammingphp

要从 PHP 文件中调用 Python 文件,您需要使用 shell_exec 函数调用它。

例如

<?php
    $command = escapeshellcmd('/usr/custom/test.py');
    $output = shell_exec($command);
    echo $output;
?>

这将调用脚本。但在顶部的脚本中,您还需要指定解释器。因此,在您的 py 文件中,在顶部添加以下行:

#!/usr/bin/env python

或者,您也可以在执行命令时提供解释器。

例如

<?php
    $command = escapeshellcmd('python3 /usr/custom/test.py');
    $output = shell_exec($command);
    echo $output;
?>

相关文章