0

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

Stupid_Intern
  • 3,382
  • 8
  • 37
  • 74
  • Think the function is `file_get_html()` – Nigel Ren May 26 '18 at 14:59
  • @NigelRen if I try that I get errors: Warning: file_get_contents(): stream does not support seeking in C:\xampp\htdocs\Tradingview\simple_html_dom.php on line 75 Warning: file_get_contents(): Failed to seek to position -1 in the stream in C:\xampp\htdocs\Tradingview\simple_html_dom.php on line 75 Fatal error: Uncaught Error: Call to a member function find() on boolean in C:\xampp\htdocs\Tradingview\index.php:31 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Tradingview\index.php on line 31 – Stupid_Intern May 26 '18 at 15:01

1 Answers1

1

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>';
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks but in some code snippets online I don't see people using curl. They are just using simple_html_dom so why is curl required? – Stupid_Intern May 26 '18 at 15:06
  • Although they were either passing html code snippets or some local html file not url. – Stupid_Intern May 26 '18 at 15:07
  • Seems like https://stackoverflow.com/questions/42685814/file-get-contents-stream-does-not-support-seeking-when-was-php-behavior-abo has some details – Nigel Ren May 26 '18 at 15:07