I trying to get the "link" elements from certain webpages. I can't figure out what i'm doing wrong though. I'm getting the following error:
Severity: Warning
Message: DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseEntityRef: no name in Entity, line: 536
Filename: controllers/test.php
Line Number: 34
Line 34 is the following in the code:
$dom->loadHTML($html);
my code:
$url = "http://www.amazon.com/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
if($html = curl_exec($ch)){
// parse the html into a DOMDocument
$dom = new DOMDocument();
$dom->recover = true;
$dom->strictErrorChecking = false;
$dom->loadHTML($html);
$hrefs = $dom->getElementsByTagName('a');
echo "<pre>";
print_r($hrefs);
echo "</pre>";
curl_close($ch);
}else{
echo "The website could not be reached.";
}
This may be caused by a rogue &
symbol that is immediately succeeded by a proper tag. As otherwise you would receive a missing ;
error. See: Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity,.
The solution is to - replace the &
symbol with &
or if you must have that &
as it is then, may be you could enclose it in: <![CDATA[
- ]]>
It means some of the HTML code is invalid. THis is just a warning, not an error. Your script will still process it. To suppress the warnings set
libxml_use_internal_errors(true);
Or you could just completely suppress the warning by doing
@$dom->loadHTML($html);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With