0

I'd like to create a web API of some kind (I don't have a preference for the protocol), where the server uses Java and the client uses PHP.

I want the request and response to both be objects (instances of classes, not JSON-style hashes). The objects' fields can be primitive types or other objects. I would define all the necessary classes in both the client and server code. PHP and Java have similar object models, so it shouldn't be hard to write corresponding classes in both languages.

To make this work, there would need to be some automated way to serialize an object on one side, and unserialize it on the other. It would need to know which PHP class maps to which Java class, and how to convert the fields. I could write something, but is there an existing protocol for transferring objects like this? Can this be done with SOAP?

JW.
  • 50,691
  • 36
  • 115
  • 143

3 Answers3

2

Java and PHP objects are not interchangeable. You will have to define the object types on both ends, and the transfer protocol could be anything you like. Serialization and deserialization makes the whole process transparent. The transport medium could be JSON, XML, YAML, or anything else for that matter.

For a record-like objects: {"_type":"MyCoolObjectType", "a":1, "b":2, "c":3"}

If you're wanting to write once and use everywhere, I'd recommend using the same language on both ends, otherwise you'll have to have a compiler that can translate between your choice languages.

Nathan
  • 31
  • 1
1

A SOAP web service can handle the basic abstraction as long as the request/response is not very complex. You can create the classes in java and then get the API to export a WSDL for them.

Chris Nava
  • 6,614
  • 3
  • 25
  • 31
0

You need to have them both serialize to the same string. The PHP format and Java format for serialization are different, and therefore incompatible. You need a common exchange format, and I recommend that you DON'T use PHP's. However, the functions to serialize in PHP are fairly simple, are contained in ext/standard/var.c file in the PHP source if you choose to use it..

See the following:

From http://en.wikipedia.org/wiki/XML (emphasis mine):

Although the design of XML focuses on documents, it is widely used for the representation of arbitrary data structures, for example in web services.

Community
  • 1
  • 1
Levi Morrison
  • 19,116
  • 7
  • 65
  • 85