Php Proc_open()c++/python 我试图用PROGOPENT调用一个C++/Python文件(需要双向支持)。 在做了一些在线研究之后,我发现创建了这个代码:(我第一次尝试用C++,失败后我也尝试了Python)

Php Proc_open()c++/python 我试图用PROGOPENT调用一个C++/Python文件(需要双向支持)。 在做了一些在线研究之后,我发现创建了这个代码:(我第一次尝试用C++,失败后我也尝试了Python),php,c++,python,Php,C++,Python,PHP: 但遗憾的是,它一直失败,并出现同样的错误:文件不能被识别为内部或外部命令、程序或批处理文件 一些额外信息: 我在PHP5.3.0中使用了Wamp 我从proc\u close()=1中得到的返回值您误解了proc\u open()的作用。它打开一个进程,就像从命令行打开一样,例如(在Unix中)diff file1.txt file2.txtproc_open()不运行可执行文件 根据我认为您的代码来自的页面,要执行外部程序,您可以使用exec()。您可以使用此选项(前提是可执行文件位

PHP:

但遗憾的是,它一直失败,并出现同样的错误:文件不能被识别为内部或外部命令、程序或批处理文件

一些额外信息:

我在PHP5.3.0中使用了Wamp
我从
proc\u close()
=1

中得到的返回值您误解了
proc\u open()
的作用。它打开一个进程,就像从命令行打开一样,例如(在Unix中)
diff file1.txt file2.txt
proc_open()
不运行可执行文件

根据我认为您的代码来自的页面,要执行外部程序,您可以使用
exec()
。您可以使用此选项(前提是可执行文件位于正确的目录中):


注意:我不确定这是否适用于Windows可执行文件


这是假设<代码>某个程序.exe < /C>是由您发布的C++源编译的可执行文件。如果您想从PHP运行Python程序,首先祝您使用Windows好运,但您希望使用

proc_open()
调用
Python somescript.py
,就像您从命令行所做的那样。

指定可执行文件的绝对路径:

$process = proc_open('/path/to/new.exe', $descriptorspec, $pipes);
我可以通过命令行调用下面的PHP脚本,让PHP在proc_打开的情况下与Perl对话:

#!/usr/bin/php -q
$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "error.log", "a")
);

$process = proc_open('/path/to/procopen.pl ' . $argv[1],
    $descriptorspec, $pipes);

if (is_resource($process)) {

  // print pipe output
  echo stream_get_contents($pipes[1]);

  // close pipe
  fclose($pipes[1]);

  // close process
  proc_close($process);
}
下面是我的Perl脚本,它与上面的PHP脚本位于同一目录中:

#!/usr/bin/perl
print @ARGV;

但是在调用Python脚本时,我无法让PHP运行。命令行永远停止运行。这是为什么?

代码中有两个问题(至少在运行Python脚本时是这样)。首先,您没有将Python输出刷新到它的标准输出,因此它永远不会到达PHP。这会导致
fgetc()
无限阻塞。这是一个简单的解决方案,只需添加一些
flush()
调用:

#!/usr/bin/env python
import sys

print 'enter input'
sys.stdout.flush()
inp = raw_input()

print 'enter exponent'
sys.stdout.flush()
exp = raw_input()

ant = inp + exp

print ant
sys.stdout.flush()
#!/usr/bin/php
<?php
$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "error-output.txt", "a")
);

$process = proc_open('/path/to/python/script.py', $descriptorspec, $pipes);

$input = 20;
$exp = 2;
if (is_resource($process)) {
    print fgets($pipes[1]);
    fwrite($pipes[0], $input . "\n");

    print fgets($pipes[1]);
    fwrite($pipes[0], $exp . "\n");

    print fgets($pipes[1]);

    fclose($pipes[1]);
    fclose($pipes[0]);
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
} else {
    echo "No resource availeble";
}
然后,在PHP代码中,在编写Python脚本STDIN时,不会发送任何换行符。因此,Python的
raw_input()
无限期地等待换行。同样,一个简单的解决方法。只需将
“\n”
添加到
fwrite()
调用:

#!/usr/bin/env python
import sys

print 'enter input'
sys.stdout.flush()
inp = raw_input()

print 'enter exponent'
sys.stdout.flush()
exp = raw_input()

ant = inp + exp

print ant
sys.stdout.flush()
#!/usr/bin/php
<?php
$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "error-output.txt", "a")
);

$process = proc_open('/path/to/python/script.py', $descriptorspec, $pipes);

$input = 20;
$exp = 2;
if (is_resource($process)) {
    print fgets($pipes[1]);
    fwrite($pipes[0], $input . "\n");

    print fgets($pipes[1]);
    fwrite($pipes[0], $exp . "\n");

    print fgets($pipes[1]);

    fclose($pipes[1]);
    fclose($pipes[0]);
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
} else {
    echo "No resource availeble";
}

你编译了C++程序吗?
#!/usr/bin/env python
import sys

print 'enter input'
sys.stdout.flush()
inp = raw_input()

print 'enter exponent'
sys.stdout.flush()
exp = raw_input()

ant = inp + exp

print ant
sys.stdout.flush()
#!/usr/bin/php
<?php
$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "error-output.txt", "a")
);

$process = proc_open('/path/to/python/script.py', $descriptorspec, $pipes);

$input = 20;
$exp = 2;
if (is_resource($process)) {
    print fgets($pipes[1]);
    fwrite($pipes[0], $input . "\n");

    print fgets($pipes[1]);
    fwrite($pipes[0], $exp . "\n");

    print fgets($pipes[1]);

    fclose($pipes[1]);
    fclose($pipes[0]);
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
} else {
    echo "No resource availeble";
}
$ ./procopen.php 
enter input
enter exponent
202
command returned 0