simplexml_load_string =>不允许对'SimpleXMLElement'进行序列化[错误] //将xml文件内容从url传递给变量

问题描述:

这不是重复的,因为我阅读所有其他问题和答案,并且无法解决我的问题,不同simplexml_load_string =>不允许对'SimpleXMLElement'进行序列化[错误] //将xml文件内容从url传递给变量

$tcmb_gov_tr = file_get_contents("http://www.tcmb.gov.tr/kurlar/today.xml"); 
     $currency_xml = simplexml_load_string($tcmb_gov_tr); 
     $tmp_currency = array(); 
     if($currency_xml->Currency[0]['Kod']=='USD'){ 
     $_SESSION['currency']['usd_try']['buying'] = $currency_xml->Currency[0]->ForexBuying[0]; 
     $_SESSION['currency']['usd_try']['selling'] = $currency_xml->Currency[0]->ForexSelling[0]; 
     } 

代码的工作,但仍然抛出

Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in [no active file] on line 0

我不想得到这个错误我应该怎么做,我从XML值作为数组或对象,你可以建议anohter方法,或者帮助我使用该功能

+1

事实上,你的错误是从“第0行”引发的,这意味着它发生在脚本关闭 - 你是否在会话中存储$货币变量? – iainn

+0

不,我不会将它存储在会话中 – TeknolojiGezgini

+0

我不会将$货币直接存储到会话中,但我将它的值存储在会话中,为什么我不知道但在会话中存储它的值抛出该错误,谢谢iainn,如果你想创建一个答案,我会将其标记为正确答案。 未在会话中存储其值,解决了我的问题 – TeknolojiGezgini

简单最佳的解决方案

$_SESSION['currency']['usd_try']['buying'] = (string)$currency_xml->Currency[0]->ForexBuying[0]; 

我发现它保存为SimpleXMLElement对象,当我把(字符串)XML元素,会话变量存储为字符串,则没有错误了。 (string)将xml元素调用为字符串函数。