1

As the problem I've mentioned here. I'm going to try alternative way of getting an image url. I want to get the product image url from https://www.matchesfashion.com/products/Adidas-By-Stella-McCartney-Metallic-zebra-print-Primegreen-leggings-1424516 and if you inspect the product image it can be access inside a <figure></figure> element. I did some reseach and wrote this code to get content from an external webpage. But it didn't return anything.

$doc = new DOMDocument;


$doc->preserveWhiteSpace = false;


$doc->strictErrorChecking = false;
$doc->recover = true;

$doc->loadHTMLFile('https://www.matchesfashion.com/products/Adidas-By-Stella-McCartney-Metallic-zebra-print-Primegreen-leggings-1424516');

$xpath = new DOMXPath($doc);

$var = $xpath->evaluate('string(//figure[@class="iiz"])');

I just need to get the source url of that image So I can continue my Image encoding process. Thanks in advance

Devin Y
  • 137
  • 2
  • 13
  • The correct image url seems to be `https://assetsprx.matchesfashion.com/img/product/920/1424516_1.jpg` why you are not using it? if you want the whole slide, better to use a iframe. – Prafulla Kumar Sahu Sep 09 '21 at 11:18
  • @PrafullaKumarSahu That's my goal . to get that url. I only have the url of the product page from that I need to get 'https://assetsprx.matchesfashion.com/img/product/920/1424516_1.jpg' – Devin Y Sep 09 '21 at 11:39
  • 1
    then you can just search for img element with `iiz__img ` class. – Prafulla Kumar Sahu Sep 09 '21 at 11:43

1 Answers1

1

Hi There you can use bellow code to grab the image urls

 $doc = new DOMDocument;
        $doc->preserveWhiteSpace = false;
        $doc->strictErrorChecking = false;
        $doc->recover = true;

        ini_set('user_agent', 'My-Application/2.5');
        libxml_use_internal_errors(true);
        $doc->loadHTMLFile('https://www.matchesfashion.com/products/Adidas-By-Stella-McCartney-Metallic-zebra-print-Primegreen-leggings-1424516');
        $xpath = new DOMXPath($doc);
        $imgs  = $xpath->query('//*[@class="iiz__img "]');
        foreach($imgs as $img)
        {
            echo 'ImgSrc: https:' . $img->getAttribute('src') .'<br />' . PHP_EOL;
        }

Here is your desired results

ImgSrc: https://assetsprx.matchesfashion.com/img/product/920/1424516_1.jpg

ImgSrc: https://assetsprx.matchesfashion.com/img/product/920/1424516_1.jpg

atikur rahman
  • 503
  • 4
  • 15
  • @atikur rahman I tried to get more advance tag by the approach you answered. but I get null. I think I came close but seems not getting desired result. please take a look if you can https://stackoverflow.com/questions/69127818/get-background-image-url-from-xpath thanks in advance. – Devin Y Sep 10 '21 at 10:08
  • 1
    Hey @DevinY appreciate to ask for help again though its different question and regardless i helped on that link too. Good Luck – atikur rahman Sep 10 '21 at 11:52