Symfony2 serialize() 偏移量错误

Symfony2 serialize() Error at offset

我正在尝试将一个 php 数组序列化为 xml,但这里有些问题。问题也可能与 rabbitmq 相关。在将 rabbitmq 实施到系统之前有类似的错误。

代码:

$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');
}

错误:

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

500 Internal Server Error - ContextErrorException

json 部分工作正常,但序列化到 xml 会出现错误。我在这里错过了什么?

编辑: 错误堆栈跟踪的一部分。

[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)"} []

编辑2: 这里似乎还有其他事情发生。它在完整的堆栈跟踪中显示了这一点:

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

为什么会这样?

编辑3: 我在代码中添加了一些回声,就在序列化之前和之后,它打印第一个而不是第二个。

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

我也将该代码放入 try-catch 中,但它没有给出任何错误......?这里有一些阴暗的事情..

编辑4: 我用这样简单的东西尝试了序列化程序:

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

这似乎工作得很好。这让我想问:序列化程序不喜欢原始数组中的哪些字符?不幸的是我无法显示数组的内部,但如果有人在使用序列化程序时对不可接受的字符有一些想法,我很乐意听到。

序列化程序似乎不喜欢我的数组键。我的钥匙里面有这样的空格:

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

当我把键改成这个时:

array("spaced_key" => $data);

它开始起作用了。