2015-10-05 37 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); 

它開始工作。