I want to take url of images from a webpage .My code doesn't work . I couldn't find the mistake in here . It returns empty array .I tried random websites . It doesn't return any url .
function get_links($url) {
$xml = new DOMDocument();
libxml_use_internal_errors(true);
if (!$xml->loadHTML($url))
{
$errors="";
foreach (libxml_get_errors() as $error) {
$errors.=$error->message."<br/>";
}
libxml_clear_errors();
print "libxml errors:<br>$errors";
return;
}
// Empty array to hold all links to return
$links = array();
//Loop through each <img> tag in the dom and add it to the link array
foreach ($xml->getElementsByTagName('img') as $link) {
$url = $link->getAttribute('src');
if (!empty($url)) {
$links[] = $link->getAttribute('src');
}
}
//Return the links
return $links;
}