How to integrate a PHP and C program?

Let me pose my problem in a simple way. I wrote a C program that I save on my server. Now I am developing a site using PHP so that other users can also access my standalone C program.

So, here I want my PHP to accept data from users and make this input and run my C program, and then accept the results again, the C program returns to users using PHP (possibly using the same site).

Please suggest me how to do this. It would be easier for me to understand if you can tell me using a simple program. For example, my C program has a function that adds two numbers. Users can provide their input (numbers) using my Website. Then, PHP somehow interacts with the C function and returns the results to the user.

+1
source share
3 answers

Just use the php exec command. I assume that you have access to the C executable. Http://www.php.net/manual/en/function.exec.php

<?php // outputs the username that owns the running php/httpd process // (on a system with the "whoami" executable in the path) echo exec('whoami'); ?> 
+3
source

You need to create a C program that parses command line arguments, for example:

 #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { printf("%d", atoi(argv[1]) + atoi(argv[2])); return 0; } 

Then call it from PHP using exec, which can put what your C program will output to stdout in an array. See http://se2.php.net/function.exec .

0
source

Cgi-bin in Apache can execute any kind of executable on your server. PHP usually runs as an Apache module to improve performance, but Cgi-bin can also run.

You can write a Cgi script (usually in PERL) to execute your C program. You can simply write a text file with your C program and then access the same file in PHP.

0
source

Source: https://habr.com/ru/post/973886/


All Articles