I have a simple scraper that fetches HTML using DomDocument
and then displays the results.
foreach ($dom->getElementsByTagName('li') as $li) {
$key = $li->getElementsByTagName('span')->item(0)->textContent;
$value = $li->getElementsByTagName('strong')->item(0)->textContent;
$results[trim($key)] = trim($value);
}
However, if the script fails to retrieve the HTML, or the options entered are wrong, it returns
Trying to get property of non-object
$key = $li->getElementsByTagName('span')->item(0)->textContent;
How do I check if it exists?
I have tried to set that $key
line as variable and check if length is greater than 0, but even as variable it fails.