So I am trying to get some information from this webpage https://promo.pan.com.hr with curl and simple HTML dom. But unfortunately, I keep receiving an error and I can't figure it out what is wrong...
<?php
include_once("simple_html_dom.php");
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$html = file_get_contents_curl("https://promo.pan.com.hr");
foreach($html->find("p") as $element)
echo $element->innertext . '<br>';
?>
Anyone knows why I am getting this error?
Fatal error: Uncaught Error: Call to a member function find() on string in C:\Server\XAMPP\htdocs\pan\index.php:21 Stack trace: #0 {main} thrown in C:\Server\XAMPP\htdocs\pan\index.php on line 21
line 21 is:
foreach($html->find("p") as $element)
I presume file_get_contents_curl
is the function which returns $data
above the call to it.
Your problem is that [curl_exec][1]
returns a string which is the contents of the web page. I presume you are using simple_html_dom
in which case you need to convert that string into a simple_html_dom
object first:
$html = str_get_html(file_get_contents_curl("https://promo.pan.com.hr"));
or you could just use:
$html = file_get_html("https://promo.pan.com.hr");
and avoid curl altogether.