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

Asked

Viewed 242 times

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?

  • already made changes in php.ini?

  • Yes, In the address lines: C: xampp php.ini memory_limit=128M for 256M ;opcache.memory_consumption=128 for 512

  • I think you should think about using the weather API to do this. However, do a test: Instead of using print_r($html); use echo $html->plaintext; and see if the error continues.

  • The Error did not continue, I got the following return: "Petrolina weather forecast - PE - Weather Now Click on a day to see the weather forecast per hourTemp.Rainfall" In case I expected the return of an array, but all right, error solved. Thank you.

  • Ah ta... calm and I’ll create an answer.

  • I was going to create an answer... but I think it’s not convenient. I’ll show you right here:

  • According to the documentation, to get the object array, use: $html->getAllAttributes (); http://simplehtmldom.sourceforge.net/manual_api.htm

  • It is interesting to send a reply to close the post

  • I’ll formulate, calm down and...

Show 4 more comments

1 answer

2


The error happened when you tried to print the created object on the screen. This function print_r php uses output buffer and probably recursiveness, which further increases memory consumption.

This function uses the internal output buffer with this parameter, then so it can’t be used inside a callback function for ob_start().

That’s why when you used the object attribute $html->plaintext; did not generate memory burst, because it was already stored there.

In case I expected the return of an array (comment from AP)

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

$html->getAllAttributes (); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.