使用SimpleXML的节点不再存在 [英] Node no longer exists using SimpleXML

查看:21
本文介绍了使用SimpleXML的节点不再存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用名为Get_Temporent的内置WordPress函数缓存XML文件,但收到php错误:

unSerialize()[unction.unSeries]:节点不再存在

//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;
}

推荐答案

您的$response_xmlSimpleXMLElement类的实例。SimpleXMLElement不应(取消)序列化,因为它将resource包装在对象中。

相反,序列化一些可以顺利通过该过程的内容;来自提要的原始响应、将其加载到SimpleXMLElement并使用asXML()方法、所需(可能是字符串)值的数组或其他可以序列化的结构后的全部/部分XML。

需要考虑的一件事是,您将在"较旧"的PHP版本中看到unserialize(): Node no longer exists警告。从PHP 5.3.2开始,行为更改为抛出Exception并显示消息Serialization of 'SimpleXMLElement' is not allowed

这篇关于使用SimpleXML的节点不再存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆