Window环境下,PHP调用Python脚本

参考

php调用python脚本***
php 调用 python脚本的方法
解决办法:php提供了许多调用其他脚本或程序的方法,比如exec/system/popen/proc_open/passthru等
window环境下,PHP调用Python脚本
推荐使用绝对路径,例如“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,

 

 

posted on   一叶舟鸣  阅读(4438)  评论(0编辑  收藏  举报

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示