Window环境下,PHP调用Python脚本
参考
解决办法:php提供了许多调用其他脚本或程序的方法,比如exec/system/popen/proc_open/passthru等
推荐使用绝对路径,例如“C:\Python27\python.exe C:\pythonwork\test1\test_php.py”,
其中“C:\Python27\python.exe” 是Python的可执行文件
“C:\pythonwork\test1\test_php.py” 是脚本
---------------------------------------------------------------------------------------------------------------------
python 路径:C:\Python27\python.exe
test_php.py 路径: C:\pythonwork\test1\test_php.py
#coding:utf-8 print 'this is a Python program with PHP,' print 'ppppppppp1,' print 'ppppppppp2,' print '哈哈哈哈哈哈,' print 'end,'
index.php
<?php echo "Hello Python !".'</br></br></br>'; //shell_exec $output = shell_exec('C:\Python27\python.exe C:\pythonwork\test1\test_php.py'); print 'shell_exec:》》 '.$output.'</br>'; print '换行输出:'.'</br>'; $array = explode(',', $output); foreach ($array as $value) { #echo "\n"; echo $value; echo "<br>"; } //exec $te0 = exec('C:\Python27\python.exe C:\pythonwork\test1\test_php.py',$out0); echo 'exec:》》 ',$te0.'</br>'; //system echo 'system:》》 '; $te1 = system('C:\Python27\python.exe C:\pythonwork\test1\test_php.py'); ?>
输出:
Hello Python ! shell_exec:》》 this is a Python program with PHP, ppppppppp1, ppppppppp2, 哈哈哈哈哈哈, end, 换行输出: this is a Python program with PHP ppppppppp1 ppppppppp2 哈哈哈哈哈哈 end exec:》》 end, system:》》 this is a Python program with PHP, ppppppppp1, ppppppppp2, 哈哈哈哈哈哈, end,