C program that connects to index.php on xampp server

977 views Asked by At

I am looking to setup a C client that can communicate with an index.php file running on a local XAMPP server.

All the tutorials online are based on creating a C client and connecting to a C server. But how would I connect a C client to a PHP file on an xampp server?

Would I need to open sockets on both the C client and also the index.php file using sockets in PHP? Or can I just open a socket in the C program and point it to say localhost/index.php?

The C client will listen for HTTP messages from the server where the index.php will be hosted. The index.php file will contain a html form where I can enter a sentence and the C program will then print that sentence into the computers terminal.

Would it be possible to connect the C program to the server. Then use the exec() command in the index.php form as a POST request and send this to the C program through a socket?

Any help or known articles would be great.

Thank you

2

There are 2 answers

0
Michael Beer On BEST ANSWER

It depends what you actually want to achieve, i.e. what you mean by 'connect' a c program to an index.php ?

The first thing you should realize is that what we are talking about here are two processes: (a) the c program and (b) whatever interpreter that executes the PHP script (Probably a web server).

You want to exchange data between two processes, thus we talk about Interprocess Communication.

In the Wikipedia article you see a table listing several means:

  • Files: Your PHP script could write to a file, the c program read from it (or, under Unix, Pipes would be even better suited)

  • Sockets: Just as you mentioned already, one of the programs could establish a connection via sockets and exchange their data over this connection.

  • other means (have a look at the table)

Since your PHP script is probably run within a web server, the socket solution would be promising: The web server already listens for incoming connections, and provides a decent protocol to exchange data: HTTP .

I will not repeat the answer by Basile , but just give an example for how to exchange some data between the c program and the PHP script with a web server using CGI:

The web server provides a URL, that will invoke the PHP script, like http://test.me/test.php .

Now, the procedure would be:

  1. The C program establishes a TCP connection to socket test.me:80
  2. The c program sends over a HTTP GET request by transmitting text in the form of

    GET /test.php?a=31&b=19 http/1.1

  3. The server will invoke the index.php, providing it with the key value pairs a=31, b=19

  4. The script will do stuff, and print something to stdout like:

    abc

(This is not a valid HTTP response, but its sufficient to scetch the procedure in general)

  1. The web server will send back the output of the php script abc over the connection to the c program.

As you see, in this example, you transferred

  1. a=31, b=19 from the c program to the web server/index.php
  2. abc from the web server/index.php to the c program

In general, the best solution depends on what you want to accomplish. I assume your example is just an example, and you could solve it the way I drafted above. However, if you really just wanted to have something printed to your console, just have your index.php write it to a file/pipe and the c program read the file...

This way or the other, I feel there is much for you to explore ...

Furthermore, I want to clarify one last thing: I used 'c program' for the sake of simplicity, but i need not be a c program, but your problem referrs to an arbitrary process, it could also be a python script run by the python interpreter or ...

0
Basile Starynkevitch On

You probably don't communicate with a PHP file, but with some Web server running that PHP program (such as Apache, part of XAMPP) for some HTTP requests. Spend several hours or days reading about HTTP (you need to understand it in some details and be aware of HTTP requests, responses, headers, cookies, etc...). Reason in terms of the HTTP protocol (what kind of HTTP requests should your C application make?). Look with your browser into the HTTP requests sent by it.

Then, what you want to use is some HTTP client library in C, like libcurl.

(Be also aware that HTTP server libraries exist in C, e.g. libonion; you probably don't need that here)

Perhaps websockets could be useful to you, but probably not.

The C client will listen for HTTP messages

If the C program is listening (with Berkeley sockets, using some listen(2) or the equivalent on your system) it is not a client but a server. If it is a client it will emit HTTP requests -using connect(2) & send(2)- (and process the corresponding HTTP responses), and you'll better use a library (like libcurl) for that (because in the details HTTP is quite complex), that will handle low level details like connect , send, recv(2) etc... and the buffering at your place.

The index.php file will contain a html form

That usually means some POST request. Details depend upon the <form HTML element.