14

I have a question regarding the titled question. So, I'm attempting to create a program which passes data/requests for data between a program in C++ and a PHP site running off of an Apache web server.

I've been researching Socket communications, but I'm not understanding the concept. I understand how to create a socket in PHP, and another in c++, and I have them running using a test application. But only individually, not talking to each other OR talking to my web server (the PHP is not on the server at the moment, it is on a different server). So how does this work? From my understanding, you need one to be listening to a port number, and the other one to send something to that command.

Ideally, I would prefer not to use any libraries to help me achieve this. I know this question has been asked many times before, but I'm still getting nowhere.

Could someone provide an explanation of how the above works, or links to a question on here/elsewhere that may be of help? Or if there is a better method of doing this than using sockets? They will be talking to each other a lot, and speed maybe an issue.

Edit, further explanation:

Web server: I'm running an Apache web server. The PHP script is located on this server.

C++ Location: While testing, my c++ application is stored on the same Raspberry Pi that the web server is running on. In the real application, my C++ application will still be stored on the same device (but it won't be a Raspberry Pi - still Linux based though).

Communication: The PHP script will need to be triggered to do things by the C++ script, and vice versa. They will need to both need to pass data (common data structures, but they could be fairly large) each way (so both need to be able to send and receive data).

halfer
  • 19,824
  • 17
  • 99
  • 186
mfisher91
  • 805
  • 1
  • 8
  • 23
  • 1
    You say that php app is on a separate server from the c++ app? Will this always be the case? – whitwhoa Oct 28 '15 at 21:32
  • 2
    This question is pretty vague. Will both programs be running on the same server? One way or two way communication? Is this a client/server type relationship? Or perhaps a worker/queue type relationship? Is php being ran behind a webserver (apache, ngnx etc)? – Steve Oct 28 '15 at 21:32
  • 1
    Hi, please elaborate on your use-case, both can communicate as long as using same protocol, if your php is on webserver, then it best be http/https (I assume C++ is client and PHP is server). If php is cli, then your option can expand to udp,tcp. You can also send email using C++ and check receive using PHP, periodically using wget and cron job, etc. – Aaron Gong Oct 28 '15 at 21:37
  • 3
    if it's web-based php, you don't need raw sockets. just get a c++ http library, and talk to php like you would with a browser/html/forms. – Marc B Oct 28 '15 at 21:56
  • Updated my question. Thanks for the help (in getting me to expand my question) – mfisher91 Oct 29 '15 at 10:19
  • @MarcB could you expand on the c++ http library at all? – mfisher91 Oct 30 '15 at 19:48
  • If they're going to need to pass fairly large data structures back and forth, and they're always going to be running on the same machine, it might make more sense to use shared memory and mutexes (or semaphores). – George Nov 05 '15 at 13:25
  • If they are on the very same machine, why you need to use php and c++. Can you explain what PHP will be doing that you cannot do via C++. This is of course for situation where you have C++ application on the same server as PHP/web server. If there is a chance they will be on different boxes, use c++ http libraries and do simple restfull requests. Example here: http://stackoverflow.com/questions/1011339/how-do-you-make-a-http-request-with-c – Wojciech Jakubas Nov 06 '15 at 12:08

6 Answers6

15

I think the easiest way is:

1) PHP -> C++ : tcp

2) C++ -> PHP : http

Will try to explain.

1) To take something from C++ app PHP connects to that app using stream_socket_client function. C++ app is listening on host and port using socket, bind and listen functions. As soon as connection arrives C++ app accept it and then in a separate std::thread serves it (recv to get request, send to send response). How to socket.

2) As PHP is working under Apache Web Server, I think the easiest way is use of libcurl library to make HTTP request from C++ app. How to curl.

Community
  • 1
  • 1
stas.yaranov
  • 1,797
  • 10
  • 17
  • Thank you for the detailed answer. I'm going to try it out later this week. I'll come back and mark it as my answer if it works well :) – mfisher91 Nov 02 '15 at 12:39
7

Another approach could be SOAP or REST Web Services. On both sides, you could provide a SOAP Web Service to exchange data or to call remote functions. On C++-side, you could use Apache Axis to provide a SOAP Server. On PHP side, you can use the build-in SOAPServer class (http://php.net/manual/de/class.soapserver.php).

With SOAP you simply would exchange XML-based messages between both applications over HTTP / HTTPS. With this approch, both applications can trigger each other and exchange data.

Ben
  • 1,579
  • 4
  • 20
  • 34
  • 1
    IMO this is the best solution because even though it takes a little work to get set up, it offers the most flexibility and if the OP decides to change one program there is the least likelihood of it breaking and the easiest method to fix it (just make sure the REST or SOAP web service is working as intended). It even allows a change to a different language. – smcjones Nov 05 '15 at 14:55
  • 1
    Apache/PHP is basically designed to use HTTP. Restful or SOAP, or JSON based equivalents are the in vogue and will solve the problem for you with minimum overhead, as you can find several frameworks that'll do most of the structural work for you. – sibaz Nov 06 '15 at 14:29
2

Adding to the answer of Ben Schnarr, I think probably the most elegant and easy solution would be to make the C++ program a client for web services implemented in the PHP code. Using sockets directly would force you to have an additional protocol over it to represent the data being transmitted (unless the data is trivially simple, like a stream of ASCII text), which would be a bit like reinventing the wheel.

I'm partial to REST+JSON, because they are simple to implement and use, and very powerful. In contrast, SOAP is quite complex, heavy in resources, and brittle, especially if you use it with C++. One popular implementation I've already used, for instance, would force you to recompile your code every time you change the layout of any of the messages, even if the server is adding a field that the client won't use.

On the PHP side, it's quite easy - it already has all the infrastructure you need. On the C++ program, the minimum you'll need would be an HTTP client library and a JSON codec. I use libcurl for the first (by far the most popular) and jsoncpp for the latter, but there are other options for both. More recently, some libraries appeared with the whole package, but I hadn't had the chance to use them yet. Some examples:

restclient

REST SDK

Restbed

Fabio Ceconello
  • 15,819
  • 5
  • 38
  • 51
1

If you need some guidance on what stas told you then I suggest you look at

http://us2.php.net/manual-lookup.php?pattern=fsock&src={referrer:source?} http://us2.php.net/manual-lookup.php?pattern=socket&src={referrer:source?}

In C++ you'll want to create a TCP listener that accepts commands (clearly you'll want to put some method of validation in), then use PHP to open a connection (use fsock or socket your choice - fsock is easier), and write data to your C++ listener. It's ezpz

//updating with some example code

// protocol://ip, port, $errno, $errstr, $timeout
$fp = fsockopen("tcp://ip", 1337, $errno, $errstr, 30);
if(!$fp) die('failed connection or something');

//write something

fwrite($fp, "stuffs to write");

// get a reply?

while (!feof($fp)) {
    echo fgets($fp, 128); // where 128 is size
}
Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
1

I recommend you to use Unix Sockets For c++ check this: http://www.linuxhowtos.org/C_C++/socket.htm

For php check this: stream_socket_client

You can pass info internally from both processes.

Hope you can solve it, don't give up.

1

A SOAP solution could be something that solve the problem, but in my opinion it complicates a lot the application deployment.

You could go down a few levels and work directly with XML-RPC.

Here you have two interesting links, that shows how to implement the client and the server in both languages.

XML-RPC for C++

XML-RPC for PHP

There are other implementatios of XML-RPC for C++ (I have not used this approach with PHP, so I don't know others alternatives), don't stick with only one, try with others and use what you feel more comfortable with.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60