我遇到了一个我似乎无法缩小范围的问题。在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-&gt; _run(&#39; / home / cillosis /... 39;)< / p>
...
这在我的视图中发生,我在访问自定义&#34;响应对象&#34;中包含的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> "
你看到&#34; ...&#34;还有很多其他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){[&#34; _querydata&#34;] =&gt; object(Tidely_QueryData)#66(9){[&#34; query&#34;] =&gt; string(40)&#34; bayou-la-batre-mississippi-sound alabama&#34; [&#34;源&#34;] =&GT; string(15)&#34; TidesController&#34; [&#34; request_type&#34;] =&GT; string(3)&#34; url&#34; [&#34; url_components&#34;] =&GT; array(2){[&#34; state&#34;] =&gt; string(7)&#34; alabama&#34; [&#34;位置&#34;] =&GT; string(32)&#34; bayou-la-batre-mississippi-sound&#34; } [&#34; return_type&#34;] =&gt;字符串(4)&#34; JSON&#34; [&#34; TIME_RANGE&#34;] =&GT; array(2){[&#34; start&#34;] =&gt; object(DateTime)#64(3){[&#34; date&#34;] =&gt; string(19)&#34; 2012-04-05 18:06:51&#34; [&#34; timezone_type&#34;] =&GT; int(3)[&#34; timezone&#34;] =&gt; string(3)&#34; UTC&#34; } [&#34; end&#34;] =&gt; object(DateTime)#63(3){[&#34; date&#34;] =&gt; string(19)&#34; 2012-04-06 18:06:51&#34; [&#34; timezone_type&#34;] =&GT; int(3)[&#34; timezone&#34;] =&gt; string(3)&#34; UTC&#34; }&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL} [&#34; _locationdata&#34;] =&gt; object(Tidely_LocationData)#80(31){[&#34; location_id&#34;] =&gt; string(5)&#34; 18921&#34; [&#34; STATION_NAME&#34;] =&GT; string(33)&#34; Bayou La Batre,Mississippi Sound&#34; [&#34; station_url_name&#34;] =&GT; string(32)&#34; bayou-la-batre-mississippi-sound&#34; [&#34; station_type&#34;] =&GT; string(11)&#34; Subordinate&#34; [&#34; station_id&#34;] =&GT; string(7)&#34; 8739051&#34; [&#34; station_lat&#34;] =&GT; string(8)&#34; + 30.3717&#34; [&#34; station_lon&#34;] =&GT; string(8)&#34; -88.2750&#34; [&#34; station_height_offset_high&#34;] =&GT; string(5)&#34; * 1.23&#34; [&#34; station_height_offset_low&#34;] =&GT; string(5)&#34; * 1.23&#34; [&#34; station_time_offset_high&#34;] =&GT; string(3)&#34; 112&#34; [&#34; station_time_offset_low&#34;] =&GT;字符串(2)&#34; 74&#34; [&#34; station_ref_station_id&#34;] =&GT; string(7)&#34; 8760551&#34; [&#34; STATE_ID&#34;] =&GT;字符串(1)&#34; 1&#34; [&#34; STATE_NAME&#34;] =&GT;字符串(7)&#34;阿拉巴马州&#34; [&#34; state_url_name&#34;] =&GT; string(7)&#34; alabama&#34; [&#34;时区&#34;] =&GT;字符串(2)&#34; -6&#34; [&#34; DST&#34;] =&GT;字符串(1)&#34; 1&#34; [&#34; CITY_NAME&#34;] =&GT; string(14)&#34; Bayou La Batre&#34; [&#34; city_zip&#34;] =&GT; string(5)&#34; 36509&#34; [&#34; city_lat&#34;] =&GT;字符串(7)&#34; 30.4014&#34; [&#34; city_lon&#34;] =&GT; string(8)&#34; -88.2467&#34; [&#34; adjacentStations&#34;] =&GT; string(321)&#34; a:10:{i:8739051; d:2.6562027890531361; s:7:&#34; TEC4393&#34 ;; 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.414028460259932; I:8742221; d:27.466706516499691; I:8731952; d: 31.251405048051701;}&#34; [&#34; city_distance&#34;] =&GT; string(15)&#34; 2.6562027890531&#34; [&#34; multipleResults&#34;] =&GT; string(0)&#34;&#34; [&#34; multipleCount&#34;] =&GT; int(0)[&#34; hasMultiple&#34;] =&gt; bool(false)[&#34; showStates&#34;] =&gt; bool(false)[&#34; showLocations&#34;] =&gt; bool(false)[&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL} [&#34; _tidedata&#34;] =&gt; object(Tidely_TideData)#68(6){[&#34; tideResults&#34;] =&gt; NULL [&#34; queryStart&#34;] =&gt; NULL [&#34; queryEnd&#34;] =&gt; NULL [&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL} [&#34; _weatherdata&#34;] =&gt; object(Tidely_WeatherData)#79(6){[&#34; weatherResults&#34;] =&gt; string(6399)&#34;气象预报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/images/xml_logo.gif http://www.weather.gov /feedback.php point1 http://forecast.weather.gov/MapClick.php?textField1=30.37&textField2=-88.28 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/images/wtf/tsra60.jpg http://forecast.weather.gov/images/wtf/tsra60.jpg http://forecast.weather.gov/images/wtf/scttsra60.jpg http://forecast.weather.gov/images/wtf/nscttsra20.jpg http://forecast.weather.gov/images/wtf/nsct.jpg& #34; [&#34; queryStart&#34;] =&GT; float(1333649211.6989)[&#34; queryEnd&#34;] =&gt; float(1333649217.2038)[&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL} [&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL}
&#34; weatherResults&#34;当我查看源时,部分有XML,我不会复制和粘贴它,因为它相当长。这是当我按下刷新并使用上述对象的缓存副本时发生的情况:
object(Tidely_ResponseData)#65(7){[&#34; _querydata&#34;] =&gt; object(Tidely_QueryData)#66(9){[&#34; query&#34;] =&gt; string(40)&#34; bayou-la-batre-mississippi-sound alabama&#34; [&#34;源&#34;] =&GT; string(15)&#34; TidesController&#34; [&#34; request_type&#34;] =&GT; string(3)&#34; url&#34; [&#34; url_components&#34;] =&GT; array(2){[&#34; state&#34;] =&gt; string(7)&#34; alabama&#34; [&#34;位置&#34;] =&GT; string(32)&#34; bayou-la-batre-mississippi-sound&#34; } [&#34; return_type&#34;] =&gt;字符串(4)&#34; JSON&#34; [&#34; TIME_RANGE&#34;] =&GT; array(2){[&#34; start&#34;] =&gt; object(DateTime)#64(3){[&#34; date&#34;] =&gt; string(19)&#34; 2012-04-05 18:10:30&#34; [&#34; timezone_type&#34;] =&GT; int(3)[&#34; timezone&#34;] =&gt; string(3)&#34; UTC&#34; } [&#34; end&#34;] =&gt; object(DateTime)#63(3){[&#34; date&#34;] =&gt; string(19)&#34; 2012-04-06 18:10:30&#34; [&#34; timezone_type&#34;] =&GT; int(3)[&#34; timezone&#34;] =&gt; string(3)&#34; UTC&#34; }&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL} [&#34; _locationdata&#34;] =&gt; object(Tidely_LocationData)#67(31){[&#34; location_id&#34;] =&gt; string(0)&#34;&#34; [&#34; STATION_NAME&#34;] =&GT; string(0)&#34;&#34; [&#34; station_url_name&#34;] =&GT; string(0)&#34;&#34; [&#34; station_type&#34;] =&GT; string(0)&#34;&#34; [&#34; station_id&#34;] =&GT; string(0)&#34;&#34; [&#34; station_lat&#34;] =&GT; string(0)&#34;&#34; [&#34; station_lon&#34;] =&GT; string(0)&#34;&#34; [&#34; station_height_offset_high&#34;] =&GT; string(0)&#34;&#34; [&#34; station_height_offset_low&#34;] =&GT; string(0)&#34;&#34; [&#34; station_time_offset_high&#34;] =&GT; string(0)&#34;&#34; [&#34; station_time_offset_low&#34;] =&GT; string(0)&#34;&#34; [&#34; station_ref_station_id&#34;] =&GT; string(0)&#34;&#34; [&#34; STATE_ID&#34;] =&GT; string(0)&#34;&#34; [&#34; STATE_NAME&#34;] =&GT; string(0)&#34;&#34; [&#34; state_url_name&#34;] =&GT; string(0)&#34;&#34; [&#34;时区&#34;] =&GT; string(0)&#34;&#34; [&#34; DST&#34;] =&GT; string(0)&#34;&#34; [&#34; CITY_NAME&#34;] =&GT; string(0)&#34;&#34; [&#34; city_zip&#34;] =&GT; string(0)&#34;&#34; [&#34; city_lat&#34;] =&GT; string(0)&#34;&#34; [&#34; city_lon&#34;] =&GT; string(0)&#34;&#34; [&#34; adjacentStations&#34;] =&GT; string(0)&#34;&#34; [&#34; city_distance&#34;] =&GT; string(0)&#34;&#34; [&#34; multipleResults&#34;] =&GT; string(0)&#34;&#34; [&#34; multipleCount&#34;] =&GT; int(0)[&#34; hasMultiple&#34;] =&gt; bool(false)[&#34; showStates&#34;] =&gt; bool(false)[&#34; showLocations&#34;] =&gt; bool(false)[&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL} [&#34; _tidedata&#34;] =&gt; object(Tidely_TideData)#68(6){[&#34; tideResults&#34;] =&gt; NULL [&#34; queryStart&#34;] =&gt; NULL [&#34; queryEnd&#34;] =&gt; NULL [&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL} [&#34; _weatherdata&#34;] =&gt; object(Tidely_WeatherData)#69(6){[&#34; weatherResults&#34;] =&gt; NULL [&#34; queryStart&#34;] =&gt; NULL [&#34; queryEnd&#34;] =&gt; NULL [&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL} [&#34; hasError&#34;] =&gt; bool(false)[&#34; userMessage&#34;] =&gt; NULL [&#34; logMessage&#34;] =&gt; NULL}
在序列化/反序列化和保存缓存的过程中,它以某种方式将所有字符串设置为空或NULL!当然,我收到XML错误,因为它是空的。
有没有其他人遇到过使用Zend Cache(或任何其他缓存库)缓存对象的问题?这与序列化有关吗?我缺少缓存设置吗?很抱歉有很长的解释,并提前感谢您的帮助!
答案 0 :(得分:6)
PHP资源无法序列化,这就是您无法序列化对象(将间接序列化SimpleXML对象)的原因。
在序列化之前,您需要将对象转换为格式良好的XML字符串(SimpleXMLElement::asXML()
),一旦您的对象被反序列化,请将您的字符串重新转换为SimpleXML对象(simplexml_load_string()
)。
See here了解更多信息。 This question也很有用。
在新细节后修改:在您的问题中使用这些新元素,我了解您的问题不是来自序列化的SimpleXML对象。你有理由不使用automatic_serialization
吗? (顺便说一下,“ i ”,我猜你拼错了)而不是让Zend_Cache自动序列化/反序列化你的对象。
编辑#2 :阅读 cillosis 的评论以获取最终解决方案。