Search code examples
phpcurlsimple-html-dom

Uncaught Error: Call to undefined function get_file_html()


I am trying to parse this webpage which works fine when I include the CURL part code after the include "simple_html_dom.php"; but if comment that part and only use simple_html_dom it gives me error saying

Uncaught Error: Call to undefined function get_file_html() in filepath\index.php:18 Stack trace: #0 {main} thrown in filepath\index.php

Do I need curl if the file is not located locally?

Sorry if this is a noob question I am just getting started with php.

<?php

    include "simple_html_dom.php";

    $html = new simple_html_dom();
    $html = get_file_html('https://in.tradingview.com/symbols/NSE-TCS/');

    foreach($html->find('a') as $element)
       echo $element->href . " -> " . $element->plaintext .'<br>'; 

?>

CURL

/*  $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://in.tradingview.com/symbols/NSE-TCS/");

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $response = curl_exec($ch);

    curl_close($ch);
    //echo $response;*/

P.S. I have downloaded simple_html_dom.php from here and the file is there in root directory


Solution

  • You can use str_get_html() with the response from the curl request you have, so the code would look something like...

    include "simple_html_dom.php";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://in.tradingview.com/symbols/NSE-TCS/");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    
    $html = str_get_html($response);
    foreach($html->find('a') as $element)   {
        echo $element->href . " -> " . $element->plaintext .'<br>';
    }