simplexml_load_file和100mb + XML文件:需要花费大量时间加载并导致超时

时间:2017-06-17 04:00:54

标签: php jquery json xml rss

我一直在使用simplexml_load_file来解析XML URL,但是,文件大小超过100mb而不是只加载节点,发生的事情是脚本在节点之前加载整个XML文件被提取和解析,导致页面TimeOut的结果。

我正在使用以下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>Boutique</title>

 </head>
  <body>



<?php header ('Content-type: text/html; charset=UTF-8'); ?>



<!--   --><link rel="stylesheet" href="/va/artigos-complexos/afilio/afilio-vitrine.css" type="text/css" />
<div><p class="ofertasdotopovitrine">Conhe&ccedil;a nossas super ofertas!</p>
</div>
<div class="mainproductebayfloatright-bottom">

<?php

function parse($url, $offset = 1, $limit = -1)
{
    $xml = simplexml_load_file($url);
    $limitCriteria = '';
    if ($limit > 0) {
        $limitCriteria = 'and position() <= ' . ((int)$offset + (int)$limit + 1);
    }

    $products = array();
    $path = sprintf('//produto[position() >= %s %s]', (int)$offset, $limitCriteria);
    foreach($xml->xpath($path) as $product) {
        $products[] = array(
          'nome' => $product->nome,
          'preco_promocao' =>  $product->preco_promocao,
          'description' => $product->descricao,
          'link_imagem' =>  $product->link_imagem,
          'link_produto' =>  $product->link_produto,
          'preco_normal' =>  $product->preco_normal,
          'parcelas' =>  $product->parcelas,
          'vl_parcelas' =>  $product->vl_parcelas  
        );
    }
    return $products;
}
//XML GOES HERE
$products = parse('http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML', 5, 5);
?>


<?php
  foreach ($products as $product) {


  print '<div class="aroundebay"><div id="aroundebay2">';
    /* */
    print '<div class="titleebay"><a target="_blank" rel="nofollow" href="'. $product['link_produto'] . '">' . $product['nome'] . '"</a></div>';
    print '<div class="mainproduct"><a target="_blank" rel="nofollow" href="' . $product['link_produto'] . '"><img style="height:120px" src="' . $product['link_imagem'] . '"/><br/>';
    //print "De:;&nbspR$". $preco_normal . "<br/>";
    print '<span>Apenas&nbspR$' . $product['preco_promocao'] . '<br/></a></span></div>';
    //print "Em&nbsp" . $parcelas . "x de&nbsp:&nbspR$" . $vl_parcelas . "</a></span></div>";           
  print '</div></div>';
}


    ?>


</div>






 </body>
</html>

CSS无关紧要。

当您使用较小的XML时,脚本可以正常工作,例如:

http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895835&token=53e355b0a09ea0.74300807&progid=1681&format=XML

是否可以仅加载xml的10个第一个节点,而不必先加载整个文件?

我也接受其他语言的建议,例如jQuery。 提前致谢。您还可以将文件格式更改为JSON和RSS,只需将format=XML更改为format=JSONformat=RSS

0 个答案:

没有答案