0

How can i get the image if I was given a URL with lots of img src? I am using Laravel 5.2.

This takes too long to load given that I have 6,000 links. Thanks in advance.

$url="$products->product_url";
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
  echo $tag->getAttribute('src');  
}
Aditya R
  • 567
  • 4
  • 17
samantha.nicole
  • 95
  • 1
  • 1
  • 11
  • You should use an `XMLReader` instead of parsing it as a DOMDocument. See http://stackoverflow.com/questions/7299957/xml-domdocument-optimization – Œlrim Oct 07 '16 at 03:47

1 Answers1

0

Firstable you need to find some kind of class of id of parent element that contain image that you need for example:

<body>
    // bla-bla-bla

    <div class="content">
        <img src="img.png" />
    </div>
</body>

You can do follow with ZendDomQuery:

$finder = new Zend_Dom_Query($html);
$node = $finder->query("div[class~='content'] img");
astratyandmitry
  • 471
  • 3
  • 7