Proc_open() c++/python - php

I'm trying to make a call for a c++ / python file with proc_open (bi-directional support needed).
After doing some on-line research I found created this code: (I first tried it with c++, after failure I tried python as well)
PHP:
<?php
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "error-output.txt", "a")
);
$process = proc_open('new.exe', $descriptorspec, $pipes);
$input = 20;
$exp = 2;
if (is_resource($process)) {
print fgets($pipes[1]);
fwrite($pipes[0], $input);
print fgets($pipes[1]);
fwrite($pipes[0], $exp);
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++:
#include <iostream>
using namespace std;
int main () {
// Get variables from php
cout << "input" << endl;
cin << input
cout << "exponent" << endl;
cin << exp
// Process variables
int answer = input + exp;
// Return variables
cout << answer;
// End c++ script
return 0;
}
Python:
print 'enter input'
inp = raw_input()
print 'enter exponent'
exp = raw_input()
ant = inp + exp
print ant
But sadly enough it kept failing with the same error: file is not recognized as internal or external command, program or batch file.
Some extra information:
I used Wamp with PHP 5.3.0
The return value I get from proc_close() = 1

There are two problems in you code (at least for running your Python script). First, you're not flushing your Python output to it's STDOUT, so it never reaches PHP. This causes fgetc() to block infinitely. It's a simple fix, just add some flush() calls:
#!/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()
Then, in your PHP code you are not sending any newlines when you write to the Python script STDIN. So, Python's raw_input() waits indefinitely for a newline. Again, an easy fix. Just add "\n" to the fwrite() calls:
#!/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";
}
And with those changes, I can make your code work as expected:
$ ./procopen.php
enter input
enter exponent
202
command returned 0

You're misunderstanding what proc_open() does. It opens a process like you would from the command line, e.g. (in Unix) diff file1.txt file2.txt. proc_open() does not run an executable.
According to the page I think you got your code from, http://php.net/manual/en/function.proc-open.php, to execute an external program you would use exec(). You can use this (provided the executable is in the right directory):
<?php
echo exec('someprogram.exe');
?>
Note: I am not certain that this will work with a Windows executable.
This is assuming that someprogram.exe is a compiled executable made from the C++ source you posted. If you wanted to run a Python program from PHP, first of all good luck using Windows, but you would want to use proc_open() to call python somescript.py, just like you would do from the command line.

Specify the absolute path of the executable file:
$process = proc_open('/path/to/new.exe', $descriptorspec, $pipes);
I can get PHP to talk to Perl with proc_open by calling the PHP script below via command line:
#!/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);
}
Here's my Perl script which is in same directory as the PHP script above:
#!/usr/bin/perl
print #ARGV;
But I can't get the PHP to run when calling your Python script. The command line just stalls forever. Why is this?

Related

php exec grep -axv don't return anything

I'am stuck since days while requesting grep on PHP, it work in cli but don't return anything via http.
it search for files that contain non UTF-8 carachters
in CLI it retrun ퟿������ but nothing (array is null) from the web
<?php
exec("/sbin/grep -axv '.*' /srv/http/test 2>&1", $datareturn);
print_r($datareturn);
?>
disable_functions = is empty in php.ini
Also tried with proc_open :
<?php
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$process = proc_open(
"/sbin/grep -axv '.*' /srv/http/test",
$descriptorspec,
$pipes
);
if (is_resource($process)) {
// Closing $pipes[0] because we don't need it
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
//avoid a deadlock
$return_value = proc_close($process);
echo "command returned $return_value\n";
}
?>
In CLI it return :
"퟿������
command returned 0
From http, "command returned 1", error-output.txt is empty in 2 cases

PHP - How to keep a Command Shell open for executing multiple commands?

Hi I am trying to execute multiple commands using PHP on a windows machine. I tried this code:
<?php
$output= shell_exec("powershell.exe");
$output2= shell_exec("write-host gokul");
shell_exec("exit");
echo( '<pre>' );
echo( $output );
echo( $output2 );
echo( '</pre>' );
?>
PHP executes each command with a new shell and closes it on completion!
I understand that each 'shell_exec' creates a new shell and control waits till command execution completes. How can I create a shell that stays open in the background till I close it ?
Am not sure if 'proc_open' or 'popen' can help ?
UPDATE
when I try PROC_OPEN()
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "error.txt", "a") // stderr is a file to write to
);
$process = proc_open('cmd.exe', $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], 'dir');
fwrite($pipes[0], 'powershell.exe');
fwrite($pipes[0], 'write-host gokul');
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
}
It prints only the prompt. Can I really use proc_open to execute multiple commands ? If so, how?
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\wamp\bin\apache\Apache2.2.17>More?
Using proc_open is the correct attempt but you need to finish every command with a new line character. Like if you would type the command manually in a shell session.
It should look like this:
fwrite($pipes[0], 'dir' . PHP_EOL);
fwrite($pipes[0], 'powershell.exe' . PHP_EOL);
fwrite($pipes[0], 'write-host gokul' . PHP_EOL);

PHP SVN update - TortoiseSVN

New code:
<?php
exec('"C:\Program Files\TortoiseSVN\bin\svn.exe" update "c:\wamp\www\project"');
This results in an infinite loop, no result is returned. What am I doing wrong?
== edit ==
On Windows, I'm trying to update a project by using PHP. I'm having problems using the commandline: I want visual feedback (important in case of conflicts), so I don't want to start as a background process. Is this possible?
The code I have so far is:
<?php
$todo = "cd \"C:\\Program Files\\TortoiseSVN\\bin\\\"";
$todo2 = "START TortoiseProc.exe /command:update /path:\"C:\\wamp\\www\\project\\\" /closeonend:0";
pclose(popen($todo, "r"));
pclose(popen($todo2, "r"));
I would drop exec and use proc_open (see http://php.net/manual/en/function.proc-open.php)
Here's an example I quickly whipped up and which should work for you:
<?php
// setup pipes that you'll use
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
// call your process
$process = proc_open('"C:\Program Files\TortoiseSVN\bin\svn.exe" update "c:\wamp\www\project"',
$descriptorspec,
$pipes);
// if process is called, pipe data to variables which can later be processed.
if(is_resource($process))
{
$stdin = stream_get_contents($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value = proc_close($process);
}
// Now it's up to you what you want to do with the data you've got.
// Remember that this is merely an example, you'll probably want to
// modify the output handling to your own likings...
header('Content-Type: text/plain; charset=UTF-8');
// check if there was an error, if not - dump the data
if($return_value === -1)
{
echo('The termination status of the process indicates an error.'."\r\n");
}
echo('---------------------------------'."\r\n".'STDIN contains:'."\r\n");
echo($stdin);
echo('---------------------------------'."\r\n".'STDOUTcontains:'."\r\n");
echo($stdout);
echo('---------------------------------'."\r\n".'STDERR contains:'."\r\n");
echo($stderr);
?>
Aside:
The line
// call your process
$process = proc_open('"C:\Program Files\TortoiseSVN\bin\svn.exe" update "c:\wamp\www\project"',
$descriptorspec,
$pipes);
could also be escaped like this
// call your process
$process = proc_open("\"C:\\Program Files\\TortoiseSVN\\bin\\svn.exe\" update \"c:\\wamp\\www\\project\"",
$descriptorspec,
$pipes);
which might or might not solve some problems on some systems that have hickups with the single brackets (') and spaces () in the notation.

Run .exe in web app and real-time communication with this .exe

My PHP web app receives data from a stream. Once the page is loaded I need to open an .exe file using system() or exec() and after short period of time new data will come, so I must type specific command to this .exe to get its returned value, how can I do this?
I'm only able to do this manually in command prompt
path/to/.exe :: hit 'Enter'
command1 params1
//...
What you're looking for is proc_open(). http://php.net/manual/en/function.proc-open.php
This will allow you to work with STDIO streams to communicate with the separate process.
Example from the PHP documentation:
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$cwd = '/tmp';
$env = array('some_option' => 'aeiou');
$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
fwrite($pipes[0], '<?php print_r($_ENV); ?>');
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
echo "command returned $return_value\n";
}
You could also consider shared memory if you need more than one listener, but this scenario sounds like you would benefit from using a queue.
Documentation msg_get_queue, msg_receive, msg_send
Example
// Send
if (msg_queue_exists(12345)) {
$mqh = msg_get_queue(12345);
$result = msg_send($mqh , 1, 'data', true);
}
// Receive
$mqh = msg_get_queue(12345, 0666);
$mqst = msg_stat_queue($mqh);
while ($mqst['msg_qnum']) {
msg_receive($mqh, 0, $msgtype, 2048, $data, true);
// Spawn your process
$mqst = msg_stat_queue($mqh);
}
Edit
Semaphore functions aren't available on Windows, as suggested above your best bet is to go with popen (unidirectional) or proc_open for bi-directional support.

Using proc_open function in PHP

I am trying to execute a TCL script from PHP. I am using PHP's proc_open for the communication .But I am unable to get the result from the tcl script .
Can someone go through the code and let me know where I am going wrong ?
PHP code
<?php
$app = 'tclsh84.exe';
$spec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w"));
$process = proc_open($app, $spec, $pipes);
if (is_resource($process))
{
fwrite($pipes[0], 'source sum.tcl ');
fwrite($pipes[0], 'tclsh test.tcl ');
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
// echo fread($pipes[1],1024).'<hr>';
proc_close($process);
}
?>
//sum.tcl
proc sum {arg1 arg2} {
set x [expr {$arg1 + $arg2}];
return $x
}
//test.tcl
puts " the sum is [sum 10 9 ] "
You're not passing newlines to the application (fwrite($pipes[0], "source sum.tcl\n")), could that be the cause? Otherwise make sure to check all return values of your function calls. You should fail early, if the first fwrite() fails, for example.

Categories