如何在 PHP Simple XML 中集成要解析的链接



我需要从XML下载所有有关庄园的照片以保存在服务器上。XMl中的每个庄园子节点都有一个包含所有信息的常规部分,然后是一个名为Foto的节点(庄园的照片(和另一个用于计划的节点(Planimetria(。每个图像的链接结构如下:

<Link>http://www.site.it/ImageView.ashx?id=[photoID]&reduce=1438[can be set as I want es: 1000, 960, 1080]</Link>

我需要在 $url_photo 和 $url_plan 中调用它,以便我可以从 XML 读取 photoID 并使用全局变量设置分辨率 (1438,1000,960(。

这是我的代码:

   <?php
    $xml = simplexml_load_file("Schede.xml"); // your xml
    $path = '/mnt/c/Users/Giuseppe/Desktop/FotoTest/';
    $i = 1;
    $resolution = '1000';

        // Estate Image
        foreach($xml->CR03_SCHEDE as $estate){
            //if((string) $estate['ELIMINATO'] = "NO"){
                echo "nEstate n $i Imagesn";
                foreach($estate->Foto->CR04_SCHEDE_FOTO as $photo){
                    $url_photo = (string) $photo->Link;
                    $filename_photo = basename($photo->CR04_FILENAME); // get the filename
                    if(file_exists($path . $filename_photo)) {
                        echo "file $filename_photo already exists n";
                    } 
                    else {
                        $img_photo = file_get_contents($url_photo); // get the image from the url
                        file_put_contents($path . $filename_photo, $img_photo); // create a file and feed the image
                        echo "file $filename_photo created n";
                    }
                }
                // Plans
                echo "nEstate n $i plansn";
                foreach($estate->Planimetria->CR04_SCHEDE_FOTO as $plan) {
                    $url_plan = (string) $plan->'http: // www.site.it/ImageView.ashx?id=' . $plan->ID . '&reduce=' . $resolution; //$plan->Link;
                    $filename_plan = basename($plan->CR04_FILENAME);
                    if(file_exists($path . $filename_plan)) {
                        echo "file planimetry $filename_plan already exists n";
                    }
                    else {
                        $img_plan = file_get_contents($url_plan); // get the image from the url
                        file_put_contents($path . $filename_plan, $img_plan); // create a file and feed the image
                        echo "file planimetry $filename_plan created n";
                    }
                }
                $i++;

/*}
            else{
                echo "$estate->attributes(Riferimento)"."Deletedn";
            }*/
        }

    ?>

如果评论,我对第一个也有问题:

if((string) $estate['ELIMINATO'] = "NO")...

Eliminato是CR03_SCHEDE的属性,但脚本不会读取它,并且在任何情况下都会进入if。完整的 XML 有大约 70/80 个属性,foreach 可以很好地下载所有图像,但我需要它应该下载唯一具有该属性等于 NO

的图像

这是 XML 的示例(只有一个资产(:链接

感谢大家

这是一个典型的错误:

if((string) $estate['ELIMINATO'] = "NO")

您使用了赋值运算符而不是比较运算符。请使用以下确切表格:

if ('NO' == (string)$estate['ELIMINATO'])

最新更新