我正在使用 php serialize() 序列化 php 对象。但是,它会在 protected 成员变量的结果中添加空字节。然后将此结果作为消息传递到 Amazon SQS 队列。问题是 SQS 不支持消息正文中的空字节。有没有办法摆脱空字节。我必须确保结果在另一端仍然是不可序列化的。
最佳答案
我在尝试序列化对象时遇到了同样的问题。
如 Michael - sqlbot 中所述评论, base64_encode
函数正在正确处理 NUL 字节。
在“序列化”方面,您应该执行以下操作:
base64_encode(serialize($object));
在“反序列化”方面:base64_decode(unserialize($object));
如果你想知道如何 serialize
函数内部工作,你可以阅读PHP Internals Book: Serialization :
The \0 in the above serialization string are NUL bytes. As you can see private and protected members are serialized with rather peculiar names: Private properties are prefixed with \0ClassName\0 and protected properties with \0*\0. These names are the result of name mangling, which is something we’ll cover in a later section.
关于php - 避免 php serialize() 中的空字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43731118/