尝试使用名为get_transient的wordpress函数构建来缓存xml文件,但是我收到了一个php错误:
unserialize()[function.unserialize]:节点不再存在
//check the db to see if it exists ( get_transient is a WordPress function)
if (false === ($response_xml = get_transient('stats_from_xml_feed'))){
$request_url = "http://example.com/feed.xml";
$request_url = urlencode($request_url);
$response_xml = @simplexml_load_file($request_url);
//kill request if connection problem
if ($response_xml === FALSE){
exit ('could not connect');
} else {
// here we throw it into the WordPress temp DB using set_transient for 12 hours
set_transient('stats_from_xml_feed', $response_xml, 60*60*12);
//some output
$res = $response_xml;
$name = $res->name;
echo $name;
}
Run Code Online (Sandbox Code Playgroud)
你$response_xml是这个SimpleXMLElement班级的一个实例.A SimpleXMLElement不应该(un)序列化,因为它将资源包装在对象中.
相反,序列化一些能够在这个过程中幸存下来的东西; 来自feed的原始响应,将所有/部分XML加载到SimpleXMLElement并使用该asXML()方法后,您需要的(可能是字符串)值的数组,或其他一些可以序列化的结构.
需要考虑的一件事是,您将unserialize(): Node no longer exists在"较旧"(使用松散术语)版本的PHP中看到警告.从PHP 5.3.2开始,行为改变为抛出Exception消息Serialization of 'SimpleXMLElement' is not allowed.
| 归档时间: |
|
| 查看次数: |
3957 次 |
| 最近记录: |