views:

2023

answers:

11

Hey guys,

Does anyone know if it is possible, actually if it has been done, to serialize an object in php and unserialize it in Java (java-php communication). Maybe an adapter will be needed.

What do you think?

Thanks

+4  A: 

Theoretically, it's certainly possible. It's just bytes after all, and they can be parsed. Of course, the deserialized object would contain only data, not any of the PHP methods. If you want that, you'd have to rewrite the behaviour as Java classes that correspond directly with the PHP classes.

In practice, the main problem seems to be that the PHP serialization format does not seem to be formally specified - at least there is no link to a specification in the manual.

So you might have to dig through the code to understand the format.

All in all, it sounds like it would be much easier and more stable to use something like XML serialization - I'm sure both languages have libraries that faciliate this.

Michael Borgwardt
A: 

Note that there's a Java implementation of PHP. So you may be able to serialise the object and pass it to your Java-PHP instance, deserialise and then call into your Java infrastructure.

It all sounds a bit of an unholy mess, but perhaps worth looking at!

Brian Agnew
+2  A: 

You can somehow make use of PHP's var_export() function for this, which returns a parseable string representation of the object you want to serialize.

karim79
+11  A: 

PHP and Java both use their own (obviously different) serialization schemes. You could however use an interchange format both could read and write.

The two most obvious examples are XML and JSON.

There are others however such as Google Protocol Buffers.

cletus
A: 

Try xstream ( converts java objects into readable xml) to serialize and then write ur own php code to deserialize.

Thej
A: 

Use Web Services (REST, RPC, SOAP) or any other solution storing plain text that will allow you to read/rebuild the data from Java.

A: 

I remember a snippet for Drupal (PHP CMS) where this functionality was needed. Just found it, so take a look at Serialized drupal node objects to java (should work with any PHP serialized object).

Maybe you can use that. I don't know whether there are issues with newer versions of PHP.

Marcel J.
+1  A: 

The JSON format would be a good place to start. There are implementations for Java, PHP and many other languages.

While initially based on the javascript object literal notation, JSON proved convenient for lightweight data transfer between all types of systems.

zimbu668
JSON is meant to be used exclusively for Javascript - dont get things messy.
mP
Sure, but it works well to transfer primitive types and is supported well by most languages.
Matt
json.org lists 35 languanges that have implementations so I would say its not meant exclusively for Javascript.
zimbu668
A: 

Serializing an object in PHP will dump the object properties. The resulting string isn't terribly complicated.

echo serialize(
 array(1, null, "mystring", array("key"=>"value"))
);

Results in:

a:4:{i:0;i:1;i:1;N;i:2;s:8:"mystring";i:3;a:1:{s:3:"key";s:5:"value";}}

The string identifies datatypes, array lengths, array indexes and values, string lengths... Wouldn't take too much effort to reverse-engineer it and come up with your own parser, I think.

Adam Backstrom
A: 

You may be also interested in using PHP/Java bridge (http://php-java-bridge.sourceforge.net/). It has own protocol. In their site said that it's fast implementation of bridge.

ruslan
+2  A: 

There is serialized-php-parser, which is a Java implementation that can parse php-serialized objects. In general, if you have the choice, I wouldn't recommend php-serialized as an exchange format, because it isn't ascii-safe (It contains null-bytes). Go with a format like xml or json instead. If you need a bit of type-information, xmlrpc is a good choice. It has good implementations for both php and Java.

troelskn