在PHP中调用Python程序可以通过以下几种方法实现:
1. 使用`exec()`函数:
$output = exec('python /path/to/your/python/script.py');echo $output;
2. 使用`shell_exec()`函数:
$output = shell_exec('python /path/to/your/python/script.py');echo $output;
3. 使用`system()`函数:
$return_var = 0;$output = 0;system('python /path/to/your/python/script.py', $return_var, $output);echo $output;
4. 使用`proc_open()`函数进行更复杂的交互控制:

$descriptorspec = array(0 => array("pipe", "r"), // 标准输入,子进程从此管道中读取数据1 => array("pipe", "w"), // 标准输出,子进程向此管道中写入数据2 => array("pipe", "w") // 标准错误,用于写入错误输出);$process = proc_open('python /path/to/your/python/script.py', $descriptorspec, $pipes);if (is_resource($process)) {fclose($pipes); // 不需要向子进程传递任何输入,所以关闭此管道$output = stream_get_contents($pipes);fclose($pipes);$error_output = stream_get_contents($pipes);fclose($pipes);proc_close($process);echo "Output: " . $output . "\n";echo "Error output: " . $error_output . "\n";}
请确保Python解释器的路径正确,并且脚本具有正确的执行权限。如果Python脚本需要参数,可以将参数作为字符串传递给`exec()`、`shell_exec()`或`system()`函数,或者使用`proc_open()`函数时通过`descriptorspec`数组传递
