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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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' ); ?> |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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, |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步