Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 serialize() Error at offset

I'm trying to serialize an php array to xml, but something is waaay off here. Problem might be rabbitmq -related too.. Had similar errors before after implementing the rabbitmq into the system.

The code:

$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new GetSetMethodNormalizer());
$serializer = new Serializer($normalizers, $encoders);

if($return_type == "xml")
{
      // Create xml-response out of the information found, and return it:
      return $serializer->serialize($options, 'xml');
}
else if($return_type == "json")
{
      // Create json-response out of the information found, and return it:
      return $serializer->serialize($options, 'json');
}

The error:

Notice: unserialize(): Error at offset 0 of 30 bytes

500 Internal Server Error - ContextErrorException

The json-part works just fine, but serializing to xml gives the error. What am I missing here?

Edit: Part of stacktrace of the error.

[2015-10-05 12:37:13] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Notice: unserialize(): Error at offset 0 of 30 bytes" at C:\wamp\www\Projects\myproject\v.0.1\vendor\oldsound\rabbitmq-bundle\OldSound\RabbitMqBundle\RabbitMq\RpcClient.php line 63 {"exception":"[object] (Symfony\Component\Debug\Exception\ContextErrorException(code: 0): Notice: unserialize(): Error at offset 0 of 30 bytes at C:\wamp\www\Projects\myproject\v.0.1\vendor\oldsound\rabbitmq-bundle\OldSound\RabbitMqBundle\RabbitMq\RpcClient.php:63)"} []

Edit2: There seems to be something else going on in here. It shows this in the full stacktrace:

vendor\oldsound\rabbitmq-bundle\OldSound\RabbitMqBundle\RabbitMq\RpcClient.php'‌​, '63', array('msg' => object(AMQPMessage), 'messageBody' => 'error: Invalid Character Error'))

Why is that there?

Edit3: I put some echoes into the code, just before the serializing, and righ after it, and it print the first but not the second.

echo "TESTING, ROW: ".__LINE__;
$xml = $serializer->serialize($options, 'xml');
echo "TESTING, ROW: ".__LINE__;

I also put that code inside try-catch, but it's not giving any errors from that..? There is something shady going on in here..

Edit4: I tried the serializer with something simple like this:

$xml = $serializer->serialize(array("test"=>1), 'xml');

And that seems to work just fine. This makes me wanna ask: what characters doesn't the serializer like in the original array? Unfortunately I cannot show the insides of the array, but if anyone has some ideas about non-acceptable chars when using serializer, I'd be happy to hear.

like image 250
GotBatteries Avatar asked Jan 27 '26 14:01

GotBatteries


1 Answers

It seems that the serializer didn't like my array keys. I had keys that had spaces in them like this:

array("spaced key" => $data);

When I changed the keys to this:

array("spaced_key" => $data);

it started to work.

like image 170
GotBatteries Avatar answered Jan 30 '26 05:01

GotBatteries



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!