Unserialize in Java a serialized php object

前端 未结 15 998
闹比i
闹比i 2020-11-29 11: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

相关标签:
15条回答
  • 2020-11-29 11:50

    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!

    0 讨论(0)
  • 2020-11-29 11:52

    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.

    0 讨论(0)
  • 2020-11-29 11:55

    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.

    0 讨论(0)
  • 2020-11-29 12:00

    add into pom.xml

    <dependency>
        <groupId>de.ailis.pherialize</groupId>
        <artifactId>pherialize</artifactId>
        <version>1.2.1</version>
    </dependency>
    

    then in code use

    MixedArray list = Pherialize.unserialize(data).toArray(); // data is string `enter code here`
    
    0 讨论(0)
  • 2020-11-29 12:00

    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.

    0 讨论(0)
  • 2020-11-29 12:04

    Another Java project to work with the PHP serialization format is Pherialize.

    Let's say you are serializing an array like this:

    array(3) {
      [0]=>
      string(8) "A string"
      [1]=>
      int(12345)
      [2]=>
      bool(true)
    }
    

    Then you can unserialize it in Java with Pherialize like this:

    MixedArray list = Pherialize.unserialize(data).toArray();
    System.out.println("Item 1: " + list.getString(0));
    System.out.println("Item 2: " + list.getInteger(1));
    System.out.println("Item 3: " + list.getBoolean(2));
    
    0 讨论(0)
提交回复
热议问题