好的,这适用于我的linux服务器,但我正在我的PC上测试我的网站,我正在尝试让php执行我的python脚本,当然\是WINDOWS只有当我在linux上的路径改变机。
$pyscript = 'C:\\wamp\\www\\testing\\scripts\\imageHandle.py';
$python = 'C:\\Python27\\python.exe';
$filePath = 'C:\\wamp\\www\\testing\\uploads\\thumbs\\10-05-2012-523.jpeg'
exec('$python $pyscript $filePath', $output, $return );
这适用于我的linux机器,但不适用于我的Windows测试服务器。如何让它在Windows上运行?哦,这也适用于直接从Windows
中的命令提示符运行编辑:
这是解决方案,因为你可以阅读答案。
$cmd = "$python $pyscript $filePath";
exec("$cmd", $output);
就像一个魅力。将单个刻度更改为加倍。
答案 0 :(得分:7)
我觉得奇怪的是C:\\wamp\\....
的filePath可以在Linux机器上运行。
无论如何,你有没有试过逃避斜线?
该问题可能与几个方面有关,包括实现不同程序执行功能的OS特定方法。我建议只是通过不同的选项,我总是将命令放入一个变量,然后打印出变量,以便我可以看到我要运行的是什么,如果你看到两个斜杠,那么你可能知道没有需要尝试逃避斜线,但不要将其打印到您不知道的屏幕上。
尝试浏览其他流程执行功能,看看哪些功能适合您。 http://www.php.net/manual/en/ref.exec.php
我找到的那个工作MOST就像在命令行运行它一样,是反引号操作符,然后是system
尝试这样的事情。
$pyscript = 'C:\\wamp\\www\\testing\\scripts\\imageHandle.py';
$python = 'C:\\Python27\\python.exe';
$filePath = 'C:\\wamp\\www\\testing\\uploads\\thumbs\\10-05-2012-523.jpeg'
$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd`
$pyscript = 'C:\wamp\www\testing\scripts\imageHandle.py';
$python = 'C:\Python27\python.exe';
$filePath = 'C:\wamp\www\testing\uploads\thumbs\10-05-2012-523.jpeg'
$cmd = "$python $pyscript $filePath";
echo $cmd;
`$cmd`
编辑:啊哈!我只是弄明白了,你用单个刻度''围绕你的命令,这意味着里面的变量没有被替换。请改用双引号。这将解决问题,但我仍然建议在运行之前回显命令,以便您可以看到正在运行的命令,并且您将能够识别出这样的问题。
答案 1 :(得分:0)
在XAMPP SERVER中,我的index.py文件就像
from wordsegment import load, segment
load()
print(segment('morerecentlythedevelopmentwhichisapotent'))
我的index.php文件就像
<html>
<head>
<title>py script</title>
</head>
<body>
<h1>Hey There!Python Working Successfully In A PHP Page.</h1>
<?php
$python = `python index.py`;
echo $python;
?>
</body>
</html>
现在运行index.php文件。
Hope this will help you
答案 2 :(得分:0)
这是一个非常古老的线程,但是仍然出现在Google的第一个结果中,因此值得进行更新。
2020年,在Python 3.8
和Windows 10
下,正确的解决方案很简单:
<?
$output = shell_exec('C:\path\to\pythonscript.py');
print ($output);
?>
其他所有内容都将导致错误。花了我几个小时找出答案。