phpserialize is a Python library that ports the `serialize` and `unserialize` functions of PHP, allowing Python applications to encode and decode data in PHP's native serialization format. It implements the standard Python serialization interface, providing `dumps` and `loads` functions. The current version is 1.3, with the last update in 2012, making it in a maintenance phase rather than actively developed, though it officially supports Python 3.
pip install phpserializeVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic serialization and deserialization of strings and PHP arrays. It highlights the necessity of providing byte strings for input to `loads` in Python 3 and handling byte strings for output, or using `decode_strings=True` for automatic string decoding. Also includes an example of deserializing a simple PHP object using `phpobject` as an `object_hook`.
Ensure input to `loads` is `bytes`. For output, either manually decode `bytes` results (e.g., `value.decode('utf-8')`) or pass `decode_strings=True` to `loads` to get Python unicode strings directly.For PHP objects, use the `object_hook` parameter with `loads`. For simple cases, `phpserialize.phpobject` can convert PHP objects to Python dictionaries. For complex type mapping, implement a custom `object_hook` function. Be aware that Python lists/tuples will be represented as PHP arrays, which are dictionaries in Python.
Test thoroughly on your target Python version. Consider inspecting the `phpserialize3` fork if facing issues, or evaluate alternative, more actively maintained serialization libraries if strict PHP serialization compatibility is not paramount.
Encode your string data into bytes before passing it to `loads`, typically using `your_string.encode('utf-8')`.To get Python unicode strings, either manually decode the `bytes` results (e.g., `value.decode('utf-8')`) or pass `decode_strings=True` to the `loads` function: `phpserialize.loads(data, decode_strings=True)`.Ensure that the input string passed to `loads` contains only the valid PHP serialized data and no extraneous characters. Validate or sanitize the source of your serialized string.
Ensure that all keys in dictionaries you are serializing are either strings or integers to be compatible with PHP's serialization format.
No dependency data recorded yet.