I am serializing php objects using php serialize(). However, it adds null bytes in the result for protected member variables. This result is then passed as a message to an Amazon SQS queue. The problem is that SQS does not support null bytes in message body. Is there any way to get rid of the null bytes. I have to make sure the result is still unserializable at the other end.
I encountered the same problem while trying to serialize an object.
As explained in Michael - sqlbot comment, base64_encode function is properly handling the NUL bytes.
On the "serializing" side you should do:
base64_encode(serialize($object));
On the "deserializing" side:
unserialize(base64_decode($object));
If you want to know how the serialize function internally works, you can read
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.