Python cPickle deserialization from PHP?

I need to deserialize a dictionary in PHP that has been serialized using cPickle in Python.

In this particular case, I could probably just search again for the information I need, but is there a better way? Any extensions for PHP that would allow me to deserialize the entire dictionary more?

It appears to be serialized in Python as follows:

import cPickle as pickle data = { 'user_id' : 5 } pickled = pickle.dumps(data) print pickled 

The contents of such a serialization cannot be easily inserted here because it contains binary data.


Decision

Since the end of Python is Django, I ended up creating my own JSON SessionStore .

+6
json python php serialization pickle
source share
4 answers

If you want to exchange data objects between programs written in different languages, it is easier to serialize / deserialize using something like JSON instead. Most major programming languages ​​have a JSON library.

+7
source share

Can you make a system call? You can use python script to convert brine data to json:

 # pickle2json.py import sys, optparse, cPickle, os try: import json except: import simplejson as json # Setup the arguments this script can accept from the command line parser = optparse.OptionParser() parser.add_option('-p','--pickled_data_path',dest="pickled_data_path",type="string",help="Path to the file containing pickled data.") parser.add_option('-j','--json_data_path',dest="json_data_path",type="string",help="Path to where the json data should be saved.") opts,args=parser.parse_args() # Load in the pickled data from either a file or the standard input stream if opts.pickled_data_path: unpickled_data = cPickle.loads(open(opts.pickled_data_path).read()) else: unpickled_data = cPickle.loads(sys.stdin.read()) # Output the json version of the data either to another file or to the standard output if opts.json_data_path: open(opts.json_data_path, 'w').write(json.dumps(unpickled_data)) else: print json.dumps(unpickled_data) 

Thus, if you get data from a file, you can do something like this:

 <?php exec("python pickle2json.py -p pickled_data.txt", $json_data = array()); ?> 

or if you want to save it in a file:

 <?php system("python pickle2json.py -p pickled_data.txt -j p_to_j.json"); ?> 

All of the above code is probably not perfect (I'm not a PHP developer), but is something like this working for you?

+5
source share

If the brine is created using the code you showed, then it will not contain binary data — unless you call new binary data lines. See Python docs . The following code was run by Python 2.6.

 >>> import cPickle >>> data = {'user_id': 5} >>> for protocol in (0, 1, 2): # protocol 0 is the default ... print protocol, repr(cPickle.dumps(data, protocol)) ... 0 "(dp1\nS'user_id'\np2\nI5\ns." 1 '}q\x01U\x07user_idq\x02K\x05s.' 2 '\x80\x02}q\x01U\x07user_idq\x02K\x05s.' >>> 

Which of the above looks the most for what you see? Can you post the contents of the pickle file as shown by the hex editor / dumper, or something like the PHP equivalent of Python repr ()? How many items are in a typical dictionary? What data types, except for the "integer" and the "string of 8-bit bytes" (what encoding?)?

+1
source share

I had the same problem. I did not find a solution, so I created my own minimalistic port of the python module in php. Later I found the Zend Serializer Adapter PythonPickle from the Zend Framework.

0
source share

All Articles