tags:

views:

22

answers:

2

Hey, everyone

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 pyhton aswell)

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

I hope you can help me to solve this problem =)

A: 

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.

Rafe Kettler
A: 

@ Rafe Kettler

Yes, it is compiled

Thank you for explaining it.

nameless