Unserialize in Java a serialized php object

2020-01-26 10:31发布

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

15条回答
成全新的幸福
2楼-- · 2020-01-26 10:52

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.

查看更多
时光不老,我们不散
3楼-- · 2020-01-26 10:52

Like previous answers have mentioned, I would avoid PHP object serialization if possible. Use JSON (which is actually faster than serialize() in PHP), thrift or some other format that is more universal.

If you have no choice I have been working on a Jackson Module to enable reading and writing serialized PHP from Java. Jackson is a great JSON parser and since PHP serialization format is pretty similar it seemed like a good fit. It's not quite complete yet (writing is still a work in progress).

查看更多
老娘就宠你
4楼-- · 2020-01-26 10:55

A better choice is to parse php serialized string to JSONArray, this repo (https://github.com/superalsrk/PhpSerialization) may help you

查看更多
家丑人穷心不美
5楼-- · 2020-01-26 10:58

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.

查看更多
时光不老,我们不散
6楼-- · 2020-01-26 10:58

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

查看更多
在下西门庆
7楼-- · 2020-01-26 10:59

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.

查看更多
登录 后发表回答