By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,826 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,826 IT Pros & Developers. It's quick & easy.

how to call C program function from PHP

P: n/a
hi frndz,
I need to call a C program function from php code, Is that
possible, If it is ,then tell me how ?
i.e,main()
{
int callfunction();
int i=2;
callfunction(i);
}
callfucntion(int i)
{
if(i==1)
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
Rgrds
Dinesh..

Jan 25 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<ba*****************@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
hi frndz,
I need to call a C program function from php code, Is that
possible, If it is ,then tell me how ?
i.e,main()
{
int callfunction();
int i=2;
callfunction(i);
}
callfucntion(int i)
{
if(i==1)
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

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
whatever.

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

myprogram.c:
// 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
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Jan 25 '07 #2

P: n/a
balakrishnan.dinesh wrote:
I need to call a C program function from php code, Is that possible, If
it is ,then tell me how ?
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:
http://uk.php.net/manual/en/zend.creating.php

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 25 '07 #3

P: n/a
On Jan 24, 11:54 pm, "Kimmo Laine" <s...@outolempi.netwrote:
<snip>
Something like this:
in php:
echo system('myprogram 1');

myprogram.c:
// 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" - lpkhttp://outolempi.net/ahdistus/- Satunnaisesti päivittyvä nettisarjis
s...@outolempi.net | rot13(x...@bhgbyrzcv.arg)
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:
<?php
$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 "./".

--
Curtis

Jan 26 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.