How to integrate PHP and C program

Hello all…

Let me put my problem in a simple way. I have written a C program, which i will keep in my server. Now i am developing a
website using PHP, so that other users can also have access to my stand alone C program.

So here i want my PHP to take input from users and bring that input and run my C program and then again take the results of
the C program back to the users using PHP(probably using the same website itself).

Please suggest me how to do it. It would be easier for me to understand if you can tell me using simple program.
For example my C program has a function that adds two numbers. The users can provide their input (the numbers) using my
website. Then PHP will somehow interact with C function and return the results to the user.

I realize it’s probably something strange, but here is what I have.

I have an application written in C/C++. This application has Perl wrapper which was made by application’s authors using SWIG. My website is written in PHP, so I’m looking for some ways to make PHP work with C/C++ application.

The only way I can think of now is to create a CGI script (perl script) which accepts POST request from my website (AJAX request), sends it to the recognition engine through it’s Perl wrapper, gets the required data and returns the required data as a response to AJAX request.

Do you think it could be done this way? Are there any better solutions?

Not all that strange. Just use the php exec command. I’m assuming you have access to the actual C executable.
http://www.php.net/manual/en/function.exec.php

Just be -VERY VERY- careful about letting users pass info into the shell! Great way to get your system hacked.

[FPHP]escapeshellarg[/FPHP] and possibly [FPHP]escapeshellcmd[/FPHP]

Another option, if you are REALLY brave, is to write a PHP socket extension to expose the functionality of your C program to PHP as native PHP functions.

Thank you…
i have read the exec command, but still have some problems understanding it.
exec(command,return_value)
How can we specify the command line argument of C here.

If my C pgm is to compute the sum of two numbers
Can i write exec(“myPgm 3,4”, $answer)

If you have some time to spare, can you pls write a small C pgm for this.
And how am i going to get the return value from C pgm to PHP

Yes i have access to the actual C executable. But still i dont know how to write the exec command.
If my C program’s name is myPgm and it calculates sum of 2 numbers. Can i write exec command as:

exec(“myPgm 3 5”, $answer)

let me write a sample C pgm

int main(int a, int b)
{
int sum= a+b;
return sum;
}

If this is my C program how can i run it in commandline using PHP exec and how am i going to get the return value.

Please help me. I am not very skilled, so please explain in simple terms.

Or use this C program

#include <stdio.h>

int main(int argc, char *argv) {
if ( argc != 3) {
printf("Usage:
%s Integer1 Integer2
“,argv[0]);
}
else {
printf(”%s + %s = %d
",argv[1],argv[2], atoi(argv[1])+atoi(argv[2]));
}
return 0;
}

hi did you got solution for your problem. me too having same problem can you help me.

Did you READ the whole thread? There are a number of responses with suggestions to follow.

Please read the thread, attempt to implement the suggested changes. If you still have questions, please create a new thread.

THREAD CLOSED