I'm trying to download product images or get file paths in XML. I'm having trouble with the same name. Thank you.
XML Files
-<products>
-<product>
<product_code>AXX-655</product_code>
<product_name>Samsung Led TV</product_name>
-<Images>
<Image>https://max.cloud/37233/products-77d4.jpg</Image>
<Image>https://max.cloud/37233/products-254789.jpg</Image>
<Image>https://max.cloud/37233/products-f010.jpg</Image>
<Image>https://max.cloud/37233/products-8004.jpg</Image>
</Images>
-</product>
-<product>
<product_code>ACH-645</product_code>
<product_name>LG Led TV</product_name>
-<Images>
<Image>https://max.cloud/37233/products-895777.jpg</Image>
<Image>https://max.cloud/37233/products-3652.jpg</Image>
<Image>https://max.cloud/37233/products-k0120.jpg</Image>
</Images>
-</product>
-</products>
I am trying to download product images by product id or product name PHP Files
$xml = simplexml_load_file($xmllocalurl) or die("Error: Cannot create object");
foreach ($xml->children() ->children() as $child) {
$name = $child->product_name;
$url = $child->Images->Image;
$img = 'imgfiles/'.$name.'.jpg';
$url = $child->product_name;
file_put_contents($img, file_get_contents($url));
Since you are dealing with xml, you are probably better off using xpath to extract what I believe is your target data.
The below just presents the data; you can then save it as you see fit:
$items = $xml->xpath('//product');
foreach ($items as $item) {
$name = $item->xpath('./product_name')[0];
$code = $item->xpath('./product_code')[0];
$images = $item->xpath('.//Image/text()');
echo $name," " ,$code," " ;
foreach ($images as $image) {
echo $image , " ";
}
echo "\r\n";
};
output (based on your sample xml):
Samsung Led TV AXX-655 https://max.cloud/37233/products-77d4.jpg https://max.cloud/37233/products-254789.jpg https://max.cloud/37233/products-f010.jpg https://max.cloud/37233/products-8004.jpg
LG Led TV ACH-645 https://max.cloud/37233/products-895777.jpg https://max.cloud/37233/products-3652.jpg https://max.cloud/37233/products-k0120.jpg