从php 5.3升级到5.6后出现PHP错误

时间:2016-07-04 07:53:43

标签: php

我有这行代码工作正常,直到我将服务器的php版本从5.3更新到5.6。

重写一个xml文件。

我猜这行不适用于5.6,但我不知道到底出了什么问题:

$newXml->asXml('my_xml.xml');

这是错误消息:

Fatal error: Call to a member function asXml() on boolean in /usr/home/example/www/sites/all/modules/custom/inmovilla_rewrite/inmovilla_rewrite.module on line 71

我应该在这做什么改变?

更新完成功能

<?php


/**
 *  Implemetns hook_cron()
 */
function inmovilla_rewrite_cron() {

$xml_external_path = 'http://ap.apinmo.com/portal/mls/jamp623433/my_xml.xml';
$xml = file_get_contents($xml_external_path);


$pattern = '/<unico>(.*?)<\/unico>/';
$response = preg_replace_callback($pattern,function($match){
        $valueUnico = trim($match[1]);

        $valueUnico = substr($valueUnico, 0, -4);
            return '<unico>'.$valueUnico.'</unico>';
},$xml);



$searches = array();
$replacements = array();
for ($i = 1; $i <= 30; $i++) {
    $searches[] = 'foto'.$i.'>';
    $replacements[] = 'foto1>';
}
$response = str_replace( $searches, $replacements, $response ) ;


$newXml = $response;

$newXml = simplexml_load_string( $newXml );
$newXml->asXml('my_xml.xml');


}

1 个答案:

答案 0 :(得分:0)

你在布尔数据类型上调用方法:asXml(),而不是SimpleXMLElement的实例 - 你可能不会在#34;格式良好的#34; XML字符串。

见这里:http://php.net/manual/en/function.simplexml-load-string.php

  

返回类SimpleXMLElement的对象,其属性包含xml文档中保存的数据,或者失败时为FALSE。

首先应该实现一些错误检查,而不是假设你有一个SimpleXMLElement实例,例如

$newXml = simplexml_load_string($newXml);

if ($newXml instanceof SimpleXMLElement) {
    $xml = $newXml->asXml('my_xml.xml');
}

但是,您需要对$newXml / $response中的值进行进一步调试,以查看XML字符串的错误。我无法在您的问题中看到XML内容,以进一步建议PHP版本的更改为何会影响这一点。

相关问题