PHP DOM how get all image links

  dom, php

I’m trying to download pictures from the site for exercises. But something does not work for me, I do not want to display links. Can anyone help me what am I doing wrong ??

This is my code 😉

$li = 'https://gratka.pl/nieruchomosci/mieszkanie-katowice-dabrowka-mala/ob/20357919';
                $options3  = array('http' => array('method'=>"GET",
                'header'=>"Accept-language: plrn" .
                "Cookie: foo=barrn", 
                'user_agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'));
                $context3  = stream_context_create($options3);
                $text1 = file_get_contents($li, false, $context3);

                
                $dom = new DOMDocument();
                libxml_use_internal_errors(true);
                $dom->loadHTML($text1);


$query_string = '';
                 
                  
                 $divs = $dom->getElementsByTagName('span');
                 foreach ($divs as $div){
                      if(preg_match('/bgallery__imageViewerb/', $div->getAttribute('class'))) {
                          $links = $div->getElementsByTagName('img');
                          foreach($links as $link){
                               $foto = $link->getAttribute('src');
                                
                               $query_string .= ('<center><img src="'.$foto.'"> </center><br/>');
                          }
                      }
                 }
print_r($query_string);

Thanks in advance to everyone for your help.

Source: Ask PHP

LEAVE A COMMENT