serialize

首先获取 ​​serialized​​数组:

$ php -r "echo serialize(['foo' => 'bar']);"
a:1:{s:3:"foo";s:3:"bar";}

serialized对象:

$ php -r "echo serialize((object) ['baz' => 'qux']);"
O:8:"stdClass":1:{s:3:"baz";s:3:"qux";}

unserialize这些数据,可以用Python的​​phpserialize​​。

Unserialize Array

from phpserialize import unserialize

# 字符串转字节有三种方式:
data = b'a:1:{s:3:"foo";s:3:"bar";}'
# data = bytes('a:1:{s:3:"foo";s:3:"bar";}', 'utf-8')
# data = 'a:1:{s:3:"foo";s:3:"bar";}'.encode('utf-8')

output = unserialize(data)
print(output) # {b'foo': b'bar'}
print(output[b'foo']) # b'bar'

在Python3里,phpserialize.unserialize需要字节格式,而不是字符串。所以反序列化后的字典关键字和值也是字节格式。

output = unserialize(binary_data)
output = {
key.decode(): val.decode() if isinstance(val, bytes) else val
for key, val in output.items()
}
print(output) # {'foo': 'bar'}

Unserialize Object

如果要反序列化php对象,需要用到​​object_hook​​​参数,并设置为​​phpobject​​。

from phpserialize import unserialize, phpobject

binary_data = b'O:8:"stdClass":1:{s:3:"baz";s:3:"qux";}'

print(unserialize(binary_data, object_hook=phpobject)) # {b'baz': b'qux'}

转换为字符串格式:

output = output._asdict()
output = {
key.decode(): val.decode() if isinstance(val, bytes) else val
for key, val in output.items()
}
print(output) # {'baz': 'qux'}