Fatal error: Allowed memory size of 16777216 bytes Exhausted (tried to allocate 6295552 bytes) in C: \ xampp \ htdocs \ Crawler \ temperatura.php on line 11

1
<?php

require_once 'simple_html_dom.php';
class temperatura {
  public function __construct(){
        $this->getTemperatura('https://www.tempoagora.com.br/previsao-do-tempo/pe/Petrolina/');
  }
  public function getTemperatura($url){
        $html = file_get_html($url);
        echo'<pre>';
        print_r($html);

  }
}

$t = new Temperatura();
?>

How to allow PHP to return without this memory problem?

    
asked by anonymous 12.12.2018 / 17:14

1 answer

2

The error occurred when you tried to print the created object on the screen. This print_r function of php uses output buffer and probably recursion, which increases even more the memory consumption.

  

This function uses the internal output buffer with this parameter, so   so it can not be used within a callback function to   ob_start ().

So when you used the attribute of the object $html->plaintext; it did not generate a memory overflow, since it was already stored there.

In case I expected the return of an array

(AP comment)

According to documentation , to get the array of object attributes just use this function:

$html->getAllAttributes (); 
    
12.12.2018 / 18:01