Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got the following problem.

I have a C script running on linux (Ubuntu 2.6), that controls a device. I have to create a remote control (in form of a website) for this function.

I can host a website on the same server, but how do I get a PHP script to call/execute the C script?
(JS would work too,but I dont really have an idea how to work with it and am low on time)

Thanks in advance
Posted
Updated 25-Jan-22 8:02am
v4

PHP has several functions[^] for executing external programs. The choice of function mainly depends on what you want to do with the output of the program.
 
Share this answer
 
Comments
Y0UR 12-Mar-12 9:27am    
Thanks, very useful link
You can run any program including compiled C programs and shell scripts in the shell by using back quotes and assigning the output to a variable. Be careful that the program is in the PATH and display the output. If it isn't you need to modify the PATH variable or use a valid absolute or relative path to the program. My example echoes out the default PATH for verification purposes:

<?php
echo `echo $PATH`;
$result=`myProg arg1 arg2 arg3`;
echo $result; #Or you can manipulate the result
?>


I realized this is a 12 year old question but I came across it while looking for a question of my own - can PHP link to and use a binary C function call directly? The only solution I have at the moment is to wrap it in a full C program and use it as in the above example.

PHP 7.4+ has a set of functions under the category FFI - foreign function interface, that does precisely what I am looking for. It's overkill for what the questioner wants to do as they refer to a C script (no such thing), and I haven't tested myself yet. For those interested here's a tutorial on FFI.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900