【问题标题】:How to display large xml feed without putting pressure in memory php如何在不给内存 php 施加压力的情况下显示大型 xml 提要
【发布时间】:2016-05-01 06:15:03
【问题描述】:

我想显示非常大的 XML 提要,而不会对 CPU 或内存造成太大压力。因此,无法再将提要放入内存,而必须以其他方式处理。

我有两个 url 来 curl 一个是有限制的: http://pf.tradetracker.net/?aid=1&type=xml&encoding=utf-8&fid=251713&categoryType=2&additionalType=2&limit=5000

其他没有限制:http://pf.tradetracker.net/?aid=1&type=xml&encoding=utf-8&fid=251713&categoryType=2&additionalType=2

<?php
    ini_set('memory_limit', '32M');
    $feed_url = "http://pf.tradetracker.net/?aid=1&type=xml&encoding=utf-8&fid=251713&categoryType=2&additionalType=2&limit=5000";
    $c = curl_init($feed_url);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $xmlstr = curl_exec($c);
    curl_close($c);
    $xml_feed_obj = json_decode(json_encode((array) simplexml_load_string($xmlstr)), 1);    
   foreach($xml_feed_obj as $products_feed)
   {
    foreach($products_feed as $feed)
    {
        $html =  "<link href='http://localhost/task/style.css' rel='stylesheet' type='text/css' />
            <div class='rightcontainer' style='margin-top:10px;'>
                <div class='flexslider carousel'>
                  <ul class='slides'>
                    <li>
                        <img src='".$feed->imageURL."' width='80' height='80'/>
                        <div class='caption-info'>
                            <div class='caption-info-head'>
                                <div class='caption-info-head-left'>
                                    <h4>Product Name: ".$feed->name."</h4><br>
                                    <span>Price: ".$feed->price."</span>
                                </div>
                                <div class='clear'> </div>
                            </div>
                        </div>
                    </li>
                  </ul>
                </div>      
            </div>";
        ob_start();
        echo $html; 
        $return_html =  ob_get_contents() ;
    }   
 }
 echo $return_html;
?>

当我 curl url 时,显示和使用内存需要时间。

【问题讨论】:

  • json_decode(json_encode((array) 你想用这个实现什么?
  • 我希望将 xml 数据放入数组中,但这不是我的问题,我的问题是处理大型 xml 提要

标签: php xml curl feed php-ini


【解决方案1】:

尝试使用“50, 100”作为限制值。

http://pf.tradetracker.net/?aid=1&type=xml&encoding=utf-8&fid=251713&categoryType=2&additionalType=2&limit=50, 100

它将从 100 开始记录并限制为 50。因此您可以使用 ajax 调用根据您的要求显示记录。它不会一次获取所有记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2018-01-06
    • 1970-01-01
    • 2011-12-22
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多