2012-11-04 35 views
0

最近幾天,我正在更新腳本以使用Joomla 2.5。這幾乎完成了,但有一件事我還沒有解決。這是一個奇怪的。xml_parse退出頁面。沒有錯誤

該腳本有一個cron解析會員XML。要做到這一點,使用PHP函數xml_parse,如下圖所示:

if (!($fp = @$file_function($url, 'rb'))) { 
    $this->error("Cannot open {$url}"); 
    return; 
} 

while (($data = fread($fp, 8192))) { 
    if (defined ('LIBXML_BUG')) { 
    # fix voor LIBXML BUG 
    $data=str_replace("&","XMLLIBHACK",$data); 
    } 
    if (!xml_parse($this->parser, $data, feof($fp))) { 

        printf('XML error in %s at line %d column %d', 
      $url, 
        xml_get_current_line_number($this->parser), 
        xml_get_current_column_number($this->parser)); 
      unset ($this->items); 
    } 
} 
xml_parser_free($this->parser); 

正如所說的xml_parse函數問題的規定。在這一行上,整個頁面/腳本停止工作,並只返回寫在該行上面的內容。當error_reporting爲E_ALL且display_errors爲On時,它不返回任何錯誤。當故意創建錯誤時,我看到error_reporting正在工作的錯誤。解析器($ this-> parser)是在另一個文件中創建的。 Wich被加載(var_dumped $ this-> parser)。

在其中創建$這個 - >解析器的代碼(我相信這個類的magpierss叫):

function create_parser($out_enc, $in_enc, $detect) { 
     if (substr(phpversion(),0,1) == 5) { 
      $parser = $this->php5_create_parser($in_enc, $detect); 
     } 
     else { 
      $parser = $this->php4_create_parser($in_enc, $detect); 
     } 
     if ($out_enc) { 
      $this->encoding = $out_enc; 
      xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $out_enc); 
     } 

     return $parser; 
    } 

    /** 
    * Instantiate an XML parser under PHP5 
    * 
    * PHP5 will do a fine job of detecting input encoding 
    * if passed an empty string as the encoding. 
    * 
    * All hail libxml2! 
    * 
    */ 
    function php5_create_parser($in_enc, $detect) { 
     // by default php5 does a fine job of detecting input encodings 
     if(!$detect && $in_enc) { 
      return xml_parser_create($in_enc); 
     } 
     else { 
      return xml_parser_create(''); 
     } 
    } 

    /** 
    * Instaniate an XML parser under PHP4 
    * 
    * Unfortunately PHP4's support for character encodings 
    * and especially XML and character encodings sucks. As 
    * force to UTF-8 use admin settings to change this 
    */ 
    function php4_create_parser($in_enc, $detect) { 
     if ($detect) { 
      $in_enc = 'UTF-8'; 
     } 

     return xml_parser_create($in_enc); 

    } 

我的想法來解決這個問題。我嘗試了不同的編碼(ISO,UTF-8等),檢查$數據,但一切似乎文件。

一個例子XML文件可以在這裏找到:http://pastebin.com/wT1pVZLQ

+0

我試過你的腳本(使用提供的數據),也沒有打破我。我在'xml_parser_free($ this-> parser)之後執行了一些代碼行;'它工作正常。你能否寄全班,以便詳細瞭解? –

+0

你在哪裏設置了錯誤報告設置?在代碼中,或在php.ini? – jprofitt

+0

Error_reporting和顯示錯誤是在php.ini中設置的,我在代碼本身嘗試了它,但這並沒有幫助。 – Timo

回答

2

我會建議你使用更靈活的SimpleXMLElement ..所有你需要做的只是循環獲取你想要的元素。

示例使用您的XML

header('Content-Type: text/html; charset=utf-8'); 
$sxe = simplexml_load_file("log.xml", "SimpleXMLElement"); 

echo "<pre>"; 
foreach ($sxe->dataHeader as $element) { 
    foreach ($element as $key => $value) 
     echo $key, " = ", $value, PHP_EOL; 
} 

echo PHP_EOL; 
foreach ($sxe->data as $record) { 
    foreach ($record as $key => $element) { 
     foreach ($element as $key => $value) 
      echo $key, " = ", $value, PHP_EOL; 
    } 
} 

輸出

exportType = stream 
exportId = 256106 
rows = 1 
lastChecked = 2012-11-04 14:03:06.26 
lastUpdated = 2012-11-04 00:03:02.822 
parserLocale = nl_NL 
streamCurrency = EUR 
name = befit2day.nl 
description = 


recordHash = 1124208770 
url = http://clicks.m4n.nl/_c?aid=14375&adid=695437&_df=true&turl=http%3A%2F%2Fbefit2day.nl%2F 
title = Universeel krachtstation 
description = Géén verzendkosten Verwachte levertijd 5 werkdagen Universeel krachtstation In hoogte verstelbare haltersteunen Rugleuning in 6 standen te verstellen Biceps curl steun in 3 standen te verstellen Been curl in 3 standen te verstellen (te belasten tot 60 kg) Geschikt voor halterschijven met stang opening van 20 - 31 mm Ook te gebruiken voor weighted crunches (met behulp van de kabel), tot 60 kg Ook te gebruiken voor dips Maximaal belastbaar tot 280 kg (inclusief gebruikersgewicht) Bankdrukken tot 180 kg Totaal gewicht 40 kg Geleverd exclusief halterstangen en gewichten Afmetingen krachtstation (L x B x H): 180 cm x 106 cm x 90-110 cm Afmetingen rugleuning (L x B x H): 68 cm x 28 cm x 4 cm Afmetingen curl steun (L x B x H): 28 cm x 44,5 cm x 4 cm Met aan de onderzijde gummi bekleding voor bescherming van de vloer 
offerid = 
image = http://befit2day.nl/img/products/11749/103/universeel-krachtstation.jpg 
price = 149.90 
category = dagaanbieding 
subcategory = 
stock = 1 
timetoship = 
ean = 
price_shipping = 0.00 
price_old = 299.90 
vendor = 
category_path = 
publisher = befit2day.nl 
column0 = dagaanbieding 
time = 0:00 
logo = http://befit2day.nl/themes/store_4/images/logo.jpg 
merchantID = 20420 
+0

謝謝。它的工作現在! – Timo

+0

不客氣... – Baba

1

檢查你的日誌,看看有什麼解析器是投擲,和/或嘗試的ini_set(「display_errors設置」,1);

您可能正在獲取文件中與您嘗試的編碼不兼容的字符,或者它無法識別xml文件是否被正確格式化。

如果是編碼問題,請首先替換字符(如果它只有一兩個字符),然後將其解析爲xml,或者找到完全支持xml文件的編碼。

1
tail -f /var/log/apache2/error.log 

刷新網站並觀察「分段錯誤」。這通常是萬事萬物停止響應的情況。

+0

錯誤日誌不顯示任何分段錯誤或任何錯誤。它看起來像PHP只是停止執行文件... – Timo

+0

恐怕PHP不會停止,但只是崩潰,因此沒有錯誤。你使用什麼版本的PHP? –

+0

PHP版本5.2。17 – Timo

相關問題