how to call C program function from PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
hi frndz,
             I need to call a C program function from php code, Is that
possible, If it is ,then tell me how ?
                    int callfunction();
                    int i=2;
                callfucntion(int i)
                    return 2;

             This is the C program which is in directory that have php
code too, So what i need is , From my php code ,i need to call the
callfunction() from that C program  . Is that possible.
or else anyother way to call c function from php code


Re: how to call C program function from PHP

Quoted text here. Click to load it

Not possible. You can't call a function inside another program written in  
totally different language in php. What you can do is you can write a  
program in c and call the program from command line with commandline  
arguments and echo the output. Then you can in php call the program and read  
the output using the built-in system functions like exec() or system() or  

Something like this:
in php:
echo system('myprogram 1');

// Apolgize my horrible C, it's been a million(?) years since i wrote C
main(argc, argv){
    int i;
    i = atoi(argv[1]);
    j = callfunction(i);
    printf('%d', j);

This is just a very raw example, but you'll get the basic idea, right?

"Ohjelmoija  on  organismi  joka  muuttaa  kofeiinia  koodiksi" - lpk - Satunnaisesti pivittyv nettisarjis | rot13(xvzzb@bhgbyrzcv.arg)  

Re: how to call C program function from PHP

Quoted text here. Click to load it
:// Satunnaisesti päivittyvä nettisarjis
Quoted text here. Click to load it

Just a couple small nitpicks. Sorry, I've actually just begun writing
C, so I'm very engaged, and enjoy every opportunity to write in it. :-)
int main(int argc, char** argv)
  int i,j;
  i = atoi(argv[1]);
  j = callfunction(i);
  printf("%d", j);     /* you need double quotes here, single quotes
are for characters only */
  return 0;

Oh yes, something useful! If you want to store the output of a command
line call, it's pretty easy to do this:
  $cli = `my_program arg1 arg2 arg3 ...`;
Notice that back ticks are used. Also, this example assumes that your
program is in the PATH. If your program's in the same directory, and
you're on *nix, then you have to precede your program with "./".


Re: how to call C program function from PHP

balakrishnan.dinesh wrote:

Quoted text here. Click to load it

As Kimmo said, you can't call a C function as-is. One option would be, as
he says, to compile it into a standalone executable, reading the
function's parameters from the command-line arguments, and then call the
compiled executable using a system() function in PHP.

However, if your C function wishes to receive an array or object parameter
you will probably need to compile the C function as a PHP extension. PHP
extensions become available as if they were normal built-in functions of
PHP. This method is a lot more complicated, but ultimately should provide
a faster and more stable solution. It makes it easier to link against a
large C library.

Help on creating extensions is here:

Toby A Inkster BSc (Hons) ARCS
Contact Me  ~

Site Timeline