如何从PHP内调用Python文件?

要从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;
?>