在 Java 中反序列化一个序列化的 php 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/743402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unserialize in Java a serialized php object
提问by Hypnus
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.
有谁知道是否有可能,实际上如果已经完成,在 php 中序列化一个对象并在 Java 中反序列化它(java-php 通信)。也许需要一个适配器。
What do you think?
你怎么认为?
Thanks
谢谢
回答by Michael Borgwardt
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.
从理论上讲,这当然是可能的。毕竟它只是字节,它们可以被解析。当然,反序列化的对象将只包含数据,而不包含任何 PHP 方法。如果需要,您必须将行为重写为与 PHP 类直接对应的 Java 类。
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.
在实践中,主要问题似乎是 PHP 序列化格式似乎没有正式指定 - 至少手册中没有指向规范的链接。
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.
总而言之,听起来使用 XML 序列化之类的东西会更容易和更稳定 - 我相信两种语言都有促进这一点的库。
回答by Brian Agnew
Note that there's a Java implementation of PHP. So you maybe able to serialise the object and pass it to your Java-PHP instance, deserialise and then call into your Java infrastructure.
请注意,有一个PHP的Java 实现。因此,您可以序列化对象并将其传递给您的 Java-PHP 实例,反序列化然后调用您的 Java 基础设施。
It all sounds a bit of an unholy mess, but perhaps worth looking at!
这一切听起来有点乱,但也许值得一看!
回答by karim79
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.
为此,您可以以某种方式使用 PHP 的var_export()函数,它返回要序列化的对象的可解析字符串表示。
回答by cletus
PHP and Java both use their own (obviously different) serialization schemes. You could however use an interchange format both could read and write.
PHP 和 Java 都使用自己的(明显不同的)序列化方案。但是,您可以使用既可以读取又可以写入的交换格式。
The two most obvious examples are XML and JSON.
两个最明显的例子是 XML 和 JSON。
There are others however such as Google Protocol Buffers.
但是还有其他的,例如 Google Protocol Buffers。
回答by Thejesh GN
回答by Thejesh GN
Use Web Services (REST, RPC, SOAP) or any other solution storing plain text that will allow you to read/rebuild the data from Java.
使用 Web 服务(REST、RPC、SOAP)或任何其他存储纯文本的解决方案,以允许您从 Java 读取/重建数据。
回答by Marcel Hymanwerth
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).
我记得需要此功能的 Drupal (PHP CMS) 片段。刚刚找到它,所以看看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.
也许你可以使用它。我不知道较新版本的 PHP 是否存在问题。
回答by zimbu668
The JSONformat would be a good place to start. There are implementations for Java, PHPand many other languages.
该JSON格式将是一个良好的开端。有Java、PHP和许多其他语言的实现。
While initially based on the javascript object literal notation, JSON proved convenient for lightweight data transfer between all types of systems.
虽然最初基于 javascript 对象文字表示法,但 JSON 被证明对于所有类型系统之间的轻量级数据传输都很方便。
回答by Annika Backstrom
Serializing an object in PHP will dump the object properties. The resulting string isn't terribly complicated.
在 PHP 中序列化对象将转储对象属性。结果字符串并不是非常复杂。
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.
字符串标识数据类型、数组长度、数组索引和值、字符串长度……我认为对其进行逆向工程并提出您自己的解析器不会花费太多精力。
回答by ruslan
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.
您可能还对使用 PHP/Java 桥(http://php-java-bridge.sourceforge.net/)感兴趣。它有自己的协议。在他们的网站上说这是桥的快速实现。