0

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;
}
anor
  • 43
  • 1
  • 7
  • 1
    *Questions seeking debugging help (**"why isn't this code working?"**) **must** include the desired behavior, a **specific problem** or **error** and the shortest code necessary to reproduce it in the question itself. Questions **without** a clear problem statement are not useful to other readers.* – Script47 Jul 12 '16 at 23:06

1 Answers1

0

The $xml->loadHTML('<html></html>') expect html string

<?php

function get_links($url) {

    $xml = new DOMDocument();

    libxml_use_internal_errors(true);

    $html = file_get_contents($url);

    if(!$xml->loadHTML($html)) {
        $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;
}

/* $cc = get_links('type your url here'); */
Ahed Eid
  • 395
  • 4
  • 17