PHP to C++ Communication

Hey guys, I've got a small PHP script which opens a socket to my server (C++ program). What would be the best way to communication between PHP/C++ sockets? JSON? XML? boost::serialize? An API for PHP with an equivalent parsing library for C++? custom?

At the moment I'm just sending a message code (say 100, 101) then a string (say "hello") and using: (eg. "101hello")

However if the data string starts with a digit, the stream sends them to the ID variable instead of Data. If there's a fix for that problem (some kind of separator? such as "101-hello-500-zzz" would be 101, "hello", 500, "zzz") then I could survive using this method. I could quickfix that, but I would like other ideas.

I would say JSON. It's simple to parse and use. You can't use Boost.Serialization for inter-language transfer; it's too bound to C++. (Someone recently tried - there was a long thread on the Boost mailing list.) XML is, in my opinion, overkill for most things.

All the buzzt! CornedBee

"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law

Thanks for the info. I tried JSON and normally I have no problems, but it seems PHP's json_decode(), as well as jQuery's getJSON(), as well as JavaScript's eval() functions fail to decode the "data" tag (shown in the xml below). Apparently the forward slashes (/) have to be escaped (\/) and even after doing so I had no luck. It was weird because if I manually copy and paste the JSON into the script, it works fine, but as a string variable, it fails. Whatever. I decided to try boost::serialize with boost XML archive. XML works fine (even before using boost). It works well except for the bloating, and you must declare all tags and sometimes values, or else it throws exceptions (stream/undefined/etc).

id/format/command/data are the important variables. Command variable tells C++ what PHP wants. C++ fills in data variable, sends it to PHP, PHP sends it to page via AJAX, AJAX changes the data variable and sends it back to PHP via AJAX, and PHP sends it back to C++ for finalization. Sometimes certain tags aren't necessarily used, but boost requires they be defined, so I just null them out before sending back and forth then resync afterwards.