So, I’m working on a web application where all data is stored as C data structures, and is accessible via socket. The web application is written in PHP, which is why I’d like to find a way to do this in PHP. Keep in mind I have control over the PHP application only, not the way the server reads / writes data.
I apologize for the broadness of the question but I’m not looking for a very specific answer. I’m more concerned with finding a ‘best’ way to proceed via collective experience so that I don’t spend too much time chasing poor solutions.
You need to define an interchange format, also known as serialization. Then the PHP and C code can convert to and from their native data structures.
While you could write the raw data to the socket, this tends to be problematic. Unless it’s all character data, you have to deal with byte order issues.
JSON is the popular format of choice these days. Most languages these days either have built-in support for it or easily obtained libraries. XML is also a possibility, but it’s more verbose and can be harder to read when debugging.
If you want a very compact format, XDR is pretty good. See my first link to find others.