致命错误:内存不足(已分配377487360)(试图分配371200000字节)

时间:2018-12-14 18:23:41

标签: php xampp

我想使用simple_html_dom.php从包含其他URL的URL抓取数据,其中包含每个项目的详细信息

<?php
include 'simple_html_dom.php';
// Create DOM from URL or file
$url='www.example.com';
$count=0;
$Links_Array = array();
$ArrayOfDomHtml=array();

// Find all links in the first page
if(!empty($url))
{
    $html = file_get_html($url);
    foreach($html->find('.li_subject .item_link') as $element) 
    {
             $Links_Array[$count]=$element->href;
             $count++;    
    }
}


// Get  details information from every item

//  Create DOM from URLS


if(!empty($Links_Array))
{
    $count=0;
    foreach($Links_Array as $element) 
            {
                 $ArrayOfDomHtml[$count] = file_get_html($element);
                 $count++;    
            }
}

// Get the title

if(!empty($ArrayOfDomHtml))
{
    $count=0;
    foreach ($ArrayOfDomHtml as  $value) 
    {

          $array2[$count] = array('title' => $value->find('.item_subject') );
          $count++;
    }
}
foreach ($array2 as $value) {
    print_r( $value);
}
?>

我正在使用xampp服务器 我想打印$ array2的值 我有内存问题,我一直在寻找这个问题,我发现了几个答案,例如在php.ini文件中设置

  • 设置memory_limit = -1

  • realcom_cache_size = 4096k

  • realcom_cache_ttl = 120

我已经进行了所有这些操作,但仍然无法正常运行

Error

第49行是print_r($ value);

编辑

我已经编辑了这样的代码以最大程度地减少内存,但仍然无法正常工作

<?php
include 'simple_html_dom.php';
// Create DOM from URL or file

// Find all links in the first page
if(!empty($url))
{
    $html = file_get_html($url);
    foreach($html->find('.li_subject .item_link') as $element) 
    {
             $Links_Array[$count]=$element->href;
             $count++;    
    }
}


// Get  details information from every item

//  Create DOM from URLS

if(!empty($Links_Array)) {
    $count=0;
    foreach($Links_Array as $url) {
          $html = file_get_html($url);
          $DetailItem[$count] = array('title' => $html ->find('.item_subject') );
          $count++;
    }
}
print_r($DetailItem);
?>

2 个答案:

答案 0 :(得分:1)

首先应该做的事情首先是,试图使程序使用更少的 内存。

不是将地球上的所有HTML都刮到内存中,而是然后解析出您想要的特定信息,将它们组合在一起,只存储您真正想要的信息。

static_assert(std::is_same_v<
    std::decay_t<decltype(Types[hana::int_c<0>])>::type, int>);

答案 1 :(得分:-2)

我要做的是找到php.ini文件,找到该行中显示memory_limit并将其设置为2048M,以防万一是由于内存引起的。

然后,您应保存此更改并重新启动服务器(取决于您使用的是apache还是nginx服务器)。