0

I have xml file for example with this content:

<?xml version="1.0" encoding="utf-8"?>

<SHOP>
    <SHOPITEM>
        <PRODUCT>Toy</PRODUCT>
        <DESCRIPTION>Toy description.</DESCRIPTION>
        <URL>http://www.hracky-matyland.cz/product/hracky-pro-nejmensi/jezek-na-baterie/4</URL>
        <IMGURL>http://www.hracky-matyland.cz/product_images/temp/jezek.jpg</IMGURL>
        <PRICE>74</PRICE>
        <PRICE_VAT>89</PRICE_VAT>
        <DUES>0</DUES>
        <AVAILABILITY>0</AVAILABILITY>
    </SHOPITEM>
</SHOP>

And I would like to get an image from IMGURL tag and download it. Of course I have in xml much more products. The reason why I need it is that client is moving to my web and want all products move to. The best should be if I could download each image into folder which has the name as PRODUCT tag so each product will have own folder with its images. I know that is possible with cURL but I don't know how to use it. Thanks advance.

Jaroslav Klimčík
  • 4,548
  • 12
  • 39
  • 58

1 Answers1

1
$data = <<<XML
<?xml version='1.0' standalone='yes'?>
<SHOP>
    <SHOPITEM>
        <PRODUCT>Toy</PRODUCT>
        <DESCRIPTION>Toy description.</DESCRIPTION>
        <URL>http://www.hracky-matyland.cz/product/hracky-pro-nejmensi/jezek-na-baterie/4</URL>
        <IMGURL>http://www.hracky-matyland.cz/product_images/temp/jezek.jpg</IMGURL>
        <PRICE>74</PRICE>
        <PRICE_VAT>89</PRICE_VAT>
        <DUES>0</DUES>
        <AVAILABILITY>0</AVAILABILITY>
    </SHOPITEM>
</SHOP>
XML;

$source = new SimpleXMLElement($data);
echo $source->SHOPITEM[0]->IMGURL;

$data contains the XML-Data.

If you want to search for the right entry, you should use DOM / DOMXpath: http://www.php.net/manual/en/class.domxpath.php

For the download you can use cUrl or simply file_get_contents:

file_put_contents ( 'localfile.jpg', file_get_contents($source->SHOPITEM[0]->IMGURL));

Make sure, that the URL comes with the right syntax.

bastey
  • 668
  • 2
  • 11
  • 17
  • Ok, now I will get something like "http://www.example.com/product_images/temp/toy.jpg" and how can I download it? – Jaroslav Klimčík Aug 18 '13 at 10:41
  • Download it via file_get_contents or cURL. For example file_put_contents('local.jpg', file_get_contents('example.com/remote.jpg'); – bastey Aug 18 '13 at 10:42