-1

I just want to get the value of the span

that's my code:

 <?php
        include('simple_html_dom.php');
        
        $html = file_get_html('https://ru.investing.com/commodities/gold');
        
        echo $html->find("span[class=arial_26 inlineblock pid-8830-last]",0)->plaintext;
        ?>

That's my error:

Fatal error: Uncaught Error: Call to a member function find() on bool in /home/f0514538/domains/f0514538.xsph.ru/public_html/test/crypto/tovar/gold.php:6 Stack trace: #0 {main} thrown in /home/f0514458/domains/f0514458.xsph.ru/public_html/test/crypto/tovar/gold.php on line 6
 
kamazz
  • 159
  • 1
  • 2
  • 12

1 Answers1

0

The $html variable contains a boolean false instead of the HTML object you were expecting. You could have debugged this by adding a

var_dump($html);

The reason why $html is empty is that the website you're trying to download has some protections and doesn't allow to download without a valid useragent.

The easiest fix is to set your User Agent manually, with

ini_set('user_agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36');

So the whole code becomes

include('simple_html_dom.php');
ini_set('user_agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36');
$html = file_get_html('https://ru.investing.com/commodities/gold');
echo $html->find("span[class=arial_26 inlineblock pid-8830-last]", 0)->plaintext;

Please ensure you're not violating the website T&C by scraping content without consent.

Andrea Olivato
  • 2,450
  • 1
  • 18
  • 30
  • Thank you bro it is work and I understand why this error is happening – kamazz May 30 '21 at 15:59
  • Glad this helped. If the answer was correct please mark it as the correct answer to help people with similar problems in the future to quickly identify the correct solution. – Andrea Olivato May 30 '21 at 16:00