2015-10-05 36 views
3

我试图序列化一个php数组到xml,但是这里有一些waaay。问题也可能是rabbitmq相关的。在将rabbitmq应用到系统之后出现类似的错误。Symfony2 serialize()错误在偏移量

代码:

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

EDIT2: 似乎有别的东西在这里发生了。它显示在完整的堆栈跟踪中:

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

这是为什么?

EDIT3: 我把一些回声到代码,就在序列化之前和之后右击,并将其打印在第一,但不是第二。

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

我也把这段代码放在try-catch中,但是它没有给出任何错误..?也有一些是在黑幕这里发生了..

Edit4: 我试图串行像这样简单的东西:

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

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

+1

您确定错误是由上述代码块引起的吗?我怀疑一个* serializing *进程会使用* unserializing *功能。 – lxg

+0

@lxg当我将'serialize($ options,'xml');'改为'serialize($ options,'json');'时,它可以工作。我认为这与rabbitmq再次成为***洞有关。这就是为什么我把它放入标签的原因。 – GotBatteries

+0

你能捕捉到异常并给我们一个堆栈跟踪? – lxg

回答

1

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

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

当我改变了键这样的:

array("spaced_key" => $data); 

它开始工作。