致命错误:未知错误:在解析数据时,请在字符串上调用成员函数find()



因此,我试图从此网页https://promo.pan.com.hr获取一些信息,并带有卷发和简单的html dom。但是不幸的是,我一直收到一个错误,我无法弄清楚什么问题...

<?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>';
?>

有人知道为什么我会遇到这个错误?

致命错误:usfult错误:c: server xampp htdocs pan pan index.php:21 stack trace:#0 {main}插入C: server中的C: server中的c: pan index.php:pan htdocs pan index.php:c: server c: server c: sever xampp htdocs pan index.php在第21行

第21行是:

foreach($html->find("p") as $element)

i假定 file_get_contents_curl是在呼叫上方返回 $data的函数。

您的问题是[curl_exec][1]返回一个是网页内容的字符串。我认为您正在使用simple_html_dom,在这种情况下,您需要首先将该字符串转换为simple_html_dom对象:

$html = str_get_html(file_get_contents_curl("https://promo.pan.com.hr"));

,也可以使用:

$html = file_get_html("https://promo.pan.com.hr");

并完全避免卷曲。

最新更新