【问题标题】:simplexml_load_string => Serialization of 'SimpleXMLElement' is not allowed[Error] // passing xml file contents from url to variablesimplexml_load_string => 'SimpleXMLElement' 的序列化是不允许的[错误] // 将 xml 文件内容从 url 传递到变量
【发布时间】:2016-11-02 14:15:30
【问题描述】:

这不是重复的,因为我阅读了所有其他问题和答案,但无法解决我的问题,而我的问题有点不同

$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];
      }

代码有效,但仍然抛出

致命错误:带有消息的未捕获异常“异常” 'SimpleXMLElement' 的序列化是不允许的'在 [no active 文件]在第 0 行

我不想得到那个错误我该怎么办,我从 xml 中获取值作为数组或对象,你可以建议另一种方法或帮助我正确使用该函数

【问题讨论】:

  • 您的错误是从“第 0 行”抛出的事实意味着它发生在脚本关闭时 - 您是否将 $currency 变量存储在会话中?
  • 不,我不会将它存储在会话中
  • 我不直接将 $currency 存储到会话中,但我将其值存储在会话中,以及为什么我不知道但将其值存储在会话中抛出该错误,谢谢 iainn,如果你愿意,请创建一个答案我会将其标记为正确答案。不在会话中存储它的值解决了我的问题

标签: php xml simplexml


【解决方案1】:

简单的最佳解决方案

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

我发现它存储为 simplexmlelement,当我将(字符串)放在 xml 元素之前,会话变量将其存储为字符串,然后没有错误。 (string) 调用 xml 元素到字符串函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 2013-06-12
    相关资源
    最近更新 更多