views:

176

answers:

8

I'm working on a project that involves a database (My SQL), website (PHP) and a custom high performance server application (C++). The C++ application (and its accompanying client application) make up the main bulk of the project, with the database storing long term data for it. The website is primarily for displaying various statistics, and administration.

1) I want the PHP scripts and c++ application to be able to communicate in some way, since the database is only used for persistent data, and additionally the c++ application may cache some things so needs to be told to reload the data in some cases. It is highly likely they will be on different machines, and even possibly different OS's. I've been considering the idea that TCP may be the best option with some simple command - response protocol?

2) What is the best way to write the common database interface code once, and be able to use it from both the PHP website and the c++ applications?

+3  A: 

1) Use the database to communicate. The C++ application can

select * from table where some_last_modified_timestamp > '<last time checked>';

2) Use stored procedures in preference to hardcoded queries both in PHP and C++.

wallyk
Yep, don't overthink this, use the DB as the IPC.
Xepoch
+1  A: 

#1: If you're on different OSes, then TCP sounds like a decent idea.

#2: Sounds like you need a C library, and then call that from both C++ (trivial) and PHP. A search on Google returns lots of articles about writing PHP extensions in C, for example:

http://devzone.zend.com/article/1021

http://www.devarticles.com/c/a/Cplusplus/Developing-Custom-PHP-Extensions-Part-1/

Eric Seppanen
A: 

My approach is to use SWIG. I use it with python, but it supports PHP also.

Then it's very easy to use it from your script.

Other solutions could be some RPC (wich would allow to have the server and the PHP application in different places).

Tristram Gräbener
Ive used swig for writing Python extensions, but is swig going to solve the fact that the c++ server is located on a different server, potentiality on a different OS to PHP?
Fire Lancer
Directly no. However, most languages offer simple data exchange over network, without having to go through the pain of knowing how serialize your data. And therefor the whole process gets easier. However, having php on both machines isn't a very elegant solution ;)
Tristram Gräbener
+1  A: 

You might try not allowing PHP to access the database at all. Make the C++ app do all the database work, and make it serve data to the PHP site. You could run part of the C++ app as a server for the PHP to fetch reports etc from it.

Eli
Is there a sane way to expose the c++ interface to PHP then? Or will I need to build up a fully functional TCP protocol for this?
Fire Lancer
You can use something much simpler such as JSON, building it up on top of an existing protocol (in this case, HTTP).
danielkza
+1  A: 

1) I would also suggest TCP. Depending on the complexity of request-response I'd probably pick either some ad-hoc text protocol or use XML (especially suitable if responses or requests are structured and more complex). If you use XML you won't need to write your own parsers/generators. You could even try using XML-RPC but I have no practical experience with that yet.

ja.ro
+1  A: 

Best way to use the same SQL in both PHP and C++ is prepared statements.

A good way to communicate is for one to host a server (custom/soap/rest) which the other connects to. PHP can easily both host and connect, and since that code is written in C it should be easy in C++ too.

Writing a PHP extension like Eric Seppanen suggest is way beyond the scope and need of your project Id say.

OIS
+1  A: 

Use Thrift or Protobufs (possibly Avro) to declare a communication protocol and use that over a tcp socket. It'll solve your cross language problems without having to roll a custom protocol, and you end up with actual objects on both sides (statically typed for c++!). I've seen Thrift used like this very successfully.

Todd Gardner
A: 

I am a beginner here, so please excuse if my idea is terribly bad.

But why cant we just transfer XML over TCP. Have a C++ TCP server and PHP TCP Client. I think PHP has a pretty powerful socket APIs

anijhaw
The original poster did not give any details about the communication, such as which initiates communication, how complicated the communication is, or whether PHP is able to connect to the C++ server or vice versa.
wallyk
I think I misinterpreted, but the original poster was considering using TCP.
anijhaw