5

I've been looking into this for several hours, but everything I've looked at seems rather daunting. I've been using PHP for all of the simple stuff on my website so far. I'm doing a lot of statistical work, and I would like to have c++ available for the more intense calculations.

The c++ would be running locally on the same Unix machine as PHP.

Something like the following is what I'm at a loss for how to do:

<?php 
//c++ program has a counter initialized to 0
//PHP tells c++ to add 5 to the counter.  $incremented is 5
$incremented = increment_in_cpp_and_return(5);
//$incremented_again will be 7
$incremented_again = increment_in_cpp_and_return(2);
?>

Of course, I'm running some monte-carlo simulations and traversing really big trees instead of incrementing numbers, but that's not what's holding me back.

C++ just needs to listen for a number and return another number (maybe some stuff in JSON at most). It is important for the c++ to keep track of its variables between calls.

I've done a lot of reading on TCP, socket programming, etc and I'm just a little doubtful that this as complicated as the examples make it out to be. A lot of things have pointed me to this https://beej.us/guide/bgnet/html/multi/clientserver.html#simpleserver

If it really is more than 100 lines of c++, are there some popular libraries, or is there a simple implementation in another language?

Thanks!

Sandburg
  • 757
  • 15
  • 28
dcc310
  • 1,038
  • 1
  • 9
  • 13
  • @therefromhere: Done: http://stackoverflow.com/a/8639563/367456 – hakre Dec 26 '11 at 22:31
  • I'm a little confused about how the c++ keeps its state between calls. Can proc_open be used to pass via STDIN to a c++ program that is continuously running? I'm worried that it starts a new instance of the c++... – dcc310 Dec 27 '11 at 03:32
  • See my answer and comment below... – Paul Dec 27 '11 at 09:53

4 Answers4

5

If you only want to access your C++ program from PHP (or use PHP as the web frontend for your C++ code), an alternative to communicating over a socket would be to embed the C++ code into PHP as an extension.

There's a fair amount of boilerplate code associated with it, but most of it is generated for you by the ext_skel script (included in the PHP source).

Most information online about writing PHP extensions relates to using C, see Extending PHP with C++? for a couple of gotchas related to using C++ for this.

Community
  • 1
  • 1
John Carter
  • 53,924
  • 26
  • 111
  • 144
3

If your C++ is executeable, you could open it as a program, pass data to it via STDIN and pass the return value back to PHP via STDOUT. See proc_open­Docs.

Your standard C++ library should offer access to STDIN and STDOUT, so you already have what you need.

I'm a little confused about how the c++ keeps its state between calls. Can proc_open be used to pass via STDIN to a c++ program that is continuously running? I'm worried that it starts a new instance of the c++

You might be looking for a Named pipe­Wikipedia, a form of inter-process communication (see as well: What are named pipes?), that is supported by most operating systems. It's simple I/O (FIFO) and similar compared to STDIN and STDOUT. You can keep your executable in memory while the other processes can send data to it.

Another simple way is to use sockets, those are supported by PHP as well and should be with your C/C++. Sockets will work across different machines, so you can run your (memory/CPU intensive?) own executable on a dedicated server that does only the calculation for example. Just looks what suits better in your case, from the comment I read you're looking for interprocess communication.

(these are just some resources, you naturally can find more with a little research, I think for both named pipes and sockets you should be able to find source-code examples for your case)

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • @dcc310 If you do this you can call `proc_open` just once from your PHP program (and use the array of pipes for the rest of that run), so you can have one instance of the C++ program per run of the PHP program. Is that what you need? – John Carter Dec 27 '11 at 08:10
  • It was not clear to me you wanted to have you C++ executable to stay in memory. I extended the answer about two ways for interprocess communication: *named pipes* and *sockets*. – hakre Dec 27 '11 at 10:55
  • Sorry, took me a while to try things out. I was looking for more of a "server" in C++, in order to keep one C++ instance across multiple PHP scripts. The named pipe solution will work just fine for me :) For anyone else trying this, I found these links somewhat helpful: http://my.opera.com/zomg/blog/2007/08/29/php-and-named-pipes and http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-3-accepting-input-from-named-pipes/ and http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-3-accepting-input-from-named-pipes/ – dcc310 Dec 29 '11 at 05:59
2

You could try: exec()

You send the data from PHP as arguments for the C++ written program that will be executed and the program will return the output so you can use it in PHP after the C++ program's execution.

Paul
  • 20,883
  • 7
  • 57
  • 74
1

I've been using gSOAP for remote procedure calls between C++ and PHP, and if you're using PHP5, the interaction is made very easy; see http://www.php.net/manual/en/class.soapclient.php

nikolas
  • 8,707
  • 9
  • 50
  • 70