奇怪的行为序列化对象并对其进行缓存 - php

我遇到了一个似乎无法缩小范围的问题。在Zend Framework应用程序中,我使用Zend Cache缓存自定义Response对象中包含的潮汐和天气数据。最初创建数据的时间点,一切正常。我序列化并缓存它。然后,当我单击刷新并从缓存中提取数据时,出现以下错误:

消息:无法将字符串解析为XML

堆栈跟踪:

0 /home/cillosis/mysites/tidely/application/views/scripts/tides/location.phtml(38):SimpleXMLElement-> __ construct('')

1 /home/cillosis/mysites/tidely/library/Zend/View.php(108):include('/ home / cillosis / ...')

2 /home/cillosis/mysites/tidely/library/Zend/View/Abstract.php(888):Zend_View-> _ run('/ home / cillosis / ...')

...

在我的视图中,这是通过以下方式访问自定义“响应对象”中包含的XML的:

<div class="data-box">

<h3>Current Weather</h3>
<hr>
<?php

// *** THIS IS LINE 38  ***                         
$weather_XML = new SimpleXMLElement($this->response->_weatherdata->weatherResults);
$params = $weather_XML->data->parameters;

$img_path = $params->{'conditions-icon'}->{'icon-link'};

echo("<img src='".$img_path."'>");

...

这是第一次运行时对象相关部分的转储(在缓存之前):

["_weatherdata"]=>
  object(Tidely_WeatherData)#79 (6) {
    ["weatherResults"]=>
    string(6399) "<?xml version="1.0"?>
       <dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="http://graphical.weather.gov/xml/DWMLgen/schema/DWML.xsd">
          ...
       </dwml> "

在看到“ ...”的地方,还有许多其他XML数据。 XML来自NWS(国家气象服务)API,我已经通过XML验证程序运行它,并且没有显示任何错误。一旦序列化对象并缓存它,问题就会发生。这是我的Zend Cache设置的方式:

// Setup caching
$frontendOptions = array('lifeTime' => 30, 'automatic_seralization' => false);
$backendOptions = array('cache_dir' => '../application/cache');
$this->_cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

我正在使用此进行缓存和检索:

// Assign cache id
// Example query: "Chesapeake Bay Virginia"
$cache_id = 'request_results_' . $this->response->_querydata->query);
$cache_id = str_replace(' ', '_', $cache_id);
$cache_id = str_replace('-', '_', $cache_id);

// Check cache for this query
if ( ($results = $this->_cache->load($cache_id)) === false)
{
   // Cache not found, process request and generate response
   ...

   // Cache request output for specific query
   $serialized_data = serialize($this->response);
   $this->_cache->save($serialized_data, $cache_id);

   // Return processed request results
   return $this->response;
}
else
{
   // Return results from cache
   return unserialize($results);
}

一旦发出请求并将其缓存,$this->response对象的输出将如下所示:

object(Tidely_ResponseData)#65(7){[“ _querydata”] => object(Tidely_QueryData)#66(9){[“ query”] =>字符串(40)“ bayou-la-batre-mississippi-sound alabama” [“源”] =>字符串(15)“ TidesController” [“ request_type”] =>字符串(3)“ url” [“ url_components”] =>数组(2){[“状态”] =>字符串(7 )“ alabama” [“位置”] =>字符串(32)“ bayou-la-batre-mississippi-sound”} [“ return_type”] =>字符串(4)“ JSON” [“ time_range”] => array( 2){[“ start”] => object(DateTime)#64(3){[“ date”] => string(19)“ 2012-04-05 18:06:51” [“ timezone_type”] => int(3)[“ timezone”] =>字符串(3)“ UTC”} [“ end”] => object(DateTime)#63(3){[“ date”] => string(19)“ 2012- 04-06 18:06:51“ [” timezone_type“] => int(3)[” timezone“] => string(3)” UTC“}} [” hasError“] => bool(false)[” userMessage “] => NULL [” logMessage“] => NULL} [” _locationdata“] => object(Tidely_LocationData)#80(31){[”“ location_id”] => string(5)“ 18921” [“ station_name”] =>字符串(33)“密西西比声音的Bayou La Batre” [“ station_url_name”] =>字符串(32)“ bayou-la-batre-mississippipp -sound“ [” station_type“] =>字符串(11)”从属“ [” station_id“] =>字符串(7)” 8739051“ [” station_lat“] =>字符串(8)” +30.3717“ [” station_lon“ ] =>字符串(8)“ -88.2750” [“ station_height_offset_high”] =>字符串(5)“ * 1.23” [“ station_height_offset_low”] =>字符串(5)“ * 1.23” [“ station_time_offset_high”] =>字符串( 3)“ 112” [“ station_time_offset_low”] =>字符串(2)“ 74” [“ station_ref_station_id”] =>字符串(7)“ 8760551” [“ state_id”] =>字符串(1)“ 1” [“ state_name “] =>字符串(7)”阿拉巴马州“ [” state_url_name“] =>字符串(7)” alabama“ [”时区“] =>字符串(2)” -6“ [” dst“] =>字符串(1 )“ 1” [“ city_name”] =>字符串(14)“ Bayou La Batre” [“ city_zip”] =>字符串(5)“ 36509” [“ city_lat”] =>字符串(7)“ 30.4014” [“ city_lon“] =>字符串(8)” -88.2467“ [” adjacentStations“] =>字符串(321)” a:10:{i:8739051; d:2.6562027890531361; s:7:“ TEC4393”; d:8.0529290086617618; i:8740448; d:11.564338218805837; i:8735180; d:14.637787569358004; i:8741196; d:17.602722038773138; i:8734635; d:17.824002491920826; i:8733810; d:19.474837234444919; i:8737048; d:24.4140 28460259932; i:8742221; d:27.466706516499691; i:8731952; d:31.251405048051701;}“ [” city_distance“] => string(15)” 2.6562027890531“ [” multipleResults“] => string(0)”“ [” multipleCount “] => int(0)[” hasMultiple“] => bool(false)[” showStates“] => bool(false)[” showLocations“] => bool(false)[” hasError“] => bool( false)[“ userMessage”] => NULL [“ logMessage”] => NULL} [“ _tidedata”] => object(Tidely_TideData)#68(6){[“” tideResults“] => NULL [” queryStart“] = > NULL [“ queryEnd”] => NULL [“ hasError”] => bool(false)[“ userMessage”] => NULL [“ logMessage”] => NULL} [“ _weatherdata”] => object(Tidely_WeatherData)# 79(6){[“ weatherResults”] =>字符串(6399)“气象预报2012-04-05T18:06:57Z http://graphical.weather.gov/xml/气象发展实验室产品生成部门http://www.nws.noaa.gov/disclaimer.html http://www.weather.gov/ http://www.weather.gov/feedback.php point1 http://www.weather.gov/images/xml_logo.gif k-p24h-n1-1 2012-04-05T08:00:00-05:00 2012-04-05T20:00:00-05 :00 k-p24h-n1-2 2012-04-05T20:00:00-05:00 2012-04-06T09:00:00-05:00 k-p3h-n5-3 2012-04-05T13:00: 00-05:00 2012-04- 05T16:00:00-05:00 2012-04-05T19:00:00-05:00 2012-04-05T22:00:00-05:00 2012-04-06T01:00:00-05:00每日上限温度81每日最低温度63温度79 79 74 69 67露点温度66 65 64 64 61风速12 11 11 13 14风向240 260 280 300 330相对湿度64 63 71 84 81天气类型,覆盖范围和强度条件图标http://forecast.weather.gov/MapClick.php?textField1=30.37&textField2=-88.28 http://forecast.weather.gov/images/wtf/tsra60.jpg http://forecast.weather.gov/images/wtf/tsra60.jpg http://forecast.weather.gov/images/wtf/nscttsra20.jpg http://forecast.weather.gov/images/wtf/scttsra60.jpg“ [” queryStart“] = > float(1333649211.6989)[“ queryEnd”] => float(1333649217.2038)[“ hasError”] => bool(false)[“ userMessage”] => NULL [“ logMessage”] => NULL} [“ hasError”] = > bool(false)[“ userMessage”] => NULL [“ logMessage”] => NULL}

当我查看源代码时,“ weatherResults”部分中包含XML,因为它很长,所以我不会复制和粘贴它。当我按刷新并使用上述对象的缓存副本时,会发生以下情况:

object(Tidely_ResponseData)#65(7){[“ _querydata”] => object(Tidely_QueryData)#66(9){[“ query”] =>字符串(40)“ bayou-la-batre-mississippi-sound alabama” [“源”] =>字符串(15)“ TidesController” [“ request_type”] =>字符串(3)“ url” [“ url_components”] =>数组(2){[“状态”] =>字符串(7 )“ alabama” [“位置”] =>字符串(32)“ bayou-la-batre-mississippi-sound”} [“ return_type”] =>字符串(4)“ JSON” [“ time_range”] => array( 2){[“ start”] => object(DateTime)#64(3){[“ date”] => string(19)“ 2012-04-05 18:10:30” [“ timezone_type”] => int(3)[“ timezone”] =>字符串(3)“ UTC”} [“ end”] => object(DateTime)#63(3){[“ date”] => string(19)“ 2012- 04-06 18:10:30“ [” timezone_type“] => int(3)[” timezone“] => string(3)” UTC“}} [” hasError“] => bool(false)[” userMessage “] => NULL [” logMessage“] => NULL} [” _locationdata“] => object(Tidely_LocationData)#67(31){[”“ location_id”] => string(0)“” [“ [station_name”] = > string(0)“” [“ station_url_name”] =>字符串(0)“” [“ station_type”] =>字符串(0)“” [“ station_id”] =>字符串(0)“” [“ station_lat“] =>字符串(0)”“ [[” station_lon“] =>字符串(0)”“ [”“” station_height_offset_high“] =>字符串(0)”“ [” station_height_offset_low“] =>字符串(0)”“ [“ station_time_offset_high”] =>字符串(0)“” [“ [station_time_offset_low”] =>字符串(0)“” [“ [station_ref_station_id”] =>字符串(0)“” [“ state_id”] =>字符串(0) “” [“ state_name”] =>字符串(0)“” [“ state_url_name”] =>字符串(0)“” [“时区”] =>字符串(0)“” [“ dst”] =>字符串( 0)“” [“ city_name”] =>字符串(0)“” [“ city_zip”] =>字符串(0)“” [“ city_lat”] =>字符串(0)“” [“ city_lon”] => string(0)“” [“ adjacentStations”] => string(0)“” [“ city_distance”] => string(0)“” [“ multipleResults”] => string(0)“” [“ multipleCount”] => int(0)[“ hasMultiple”] => bool(false)[“ showStates”] => bool(false)[“ showLocations”] => bool(false)[“ hasError”] => bool(false) [“ userMessage”] => NULL [“ logMessage”] => NULL} [“ _tidedata”] => object(Tidely_TideData)#68(6){[“” tideResults“] => NULL [” queryStart“] => NULL [“ queryEnd”] => NULL [“ hasError”] => bool(false)[“ userMessage”] => NULL [“ lo gMessage“] => NULL} [” _weatherdata“] => object(Tidely_WeatherData)#69(6){[” weatherResults“] => NULL [” queryStart“] => NULL [” queryEnd“] => NULL [” hasError“] => bool(false)[” userMessage“] => NULL [” logMessage“] => NULL} [” hasError“] => bool(false)[” userMessage“] => NULL [” logMessage“] => NULL}

在序列化/反序列化和保存缓存的过程中,它将以某种方式将所有字符串设置为空或NULL!因此,当然,我得到了XML错误,因为它是空的。

是否有人在使用Zend Cache(或其他任何缓存库)缓存对象时遇到问题?这和序列化有关吗?我是否缺少缓存设置?抱歉,冗长的解释,并预先感谢您的帮助!

参考方案

PHP资源无法序列化,这就是为什么您不能序列化对象(它将间接序列化SimpleXML对象)的原因。

在序列化之前,您需要将对象转换为格式正确的XML字符串(SimpleXMLElement::asXML()),并且在对对象进行反序列化后,将其重新转换为SimpleXML对象(simplexml_load_string())。

See here有关更多信息。 This question也可能有用。

在新的细节之后进行编辑:在您的问题中包含了这些新元素之后,我知道您的问题并非来自于序列化的SimpleXML对象。您有什么理由不使用automatic_serialization吗? (顺便说一句,用“ i”表示,我猜错了),而不是让Zend_Cache自动序列化/反序列化对象。

编辑#2:阅读cillosis下面的注释以寻求最终解决方案。

CakePHP将数据传递到元素 - php

我的控制器中有以下代码:function index() { $posts = $this->set('posts', $this->Portfolio->find('all')); if (isset($this->params['requested'])) { retur…

phpWord中的粗体,空格和缩进文本 - php

我有一些要加粗的文本,与前几段和后几段分开并缩进。我不能让所有三个属性一起工作。这适用于粗体和空格:$section = $phpWord->addSection(); $section->addText( 'Re: Your Application for Post of Security Guard', array(�…

PHP数组可以这样做吗? - php

可以说;我有一个$ friends数组,其中有2,000个不同的friendID号+我有一个带有10,000 bulletinID号的$ bulletins数组,该$ bulletins数组还将具有另一个值,该ID的用户ID是发布公告条目的用户现在可以获取所有具有与FriendsID数组中的userID匹配的userID的bulletinID号吗?甚至有可能…

哪个更好的做法?从Jquery响应获取HTML - php

这只是一个问题,以了解人们如何以及如何做到这一点,但是假设用户向列表中添加了一些内容,完成后,它将运行下面的ajax并更新.user-stream-list$.ajax({ url: "user-stream-list.php", success: function(data){ $(".user-stream-list…

PHP-将2个数组的值连接到1个新数组中 - php

我有2个具有相同结构的独立多维数组。例:Array ( [0] => Array ( [uploadData] => 1234 Main St ) [1] => Array ( [uploadData] => 5678 Elm St ) [2] => Array ( [uploadData] => 9879 New St …