如何从PHP中调用Python文件?

admin 阅读:59 2024-03-02

如何从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;
?>
声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!