从 XML 导入数据时重复(简单 XML 解析器)PHP

提图斯

我正在尝试从 XML 中提取数据,如下所示

<list last_update="2018-12-11 17:37:18" listing_count="196">
    <property last_update="2018-12-10 15:02:21">
    <agent>
    <name>
    <![CDATA[ G. G. ]]>
    </name>
    <email>[email protected]</email>
    <phone>+971 50 355 4416</phone>
    <license_no>38729</license_no>
    </agent>
    <parking>1</parking>
    <photo>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/OWWFGpAN3l4tF5eU.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/ZNBpQDrbx7fSURP6.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/DKDkGl1DXb5Ec75E.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/aMUA4rFzFRnr0jru.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/70UFXHqM7OAo3f5h.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/sELG0dzNyOxmwlFE.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/x2S9Q0BvwT1fZUyb.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/s9MDwWwYKeloiibS.jpeg
    </url>
    <url last_update="2018-10-07 16:31:14" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/SahPWXAGL0dAMpMg.jpeg
    </url>
    <url last_update="2018-12-10 14:59:07" watermark="Yes">
    https://s3-ap-southeast-1.amazonaws.com/mycrm-pro-accounts-v2/property/full/1300/iAuYR3KfjNRIAJkk.jpeg
    </url>
    </photo>
    <geopoints>55.34016973,25.2242947</geopoints>
    </property>
</list>

我尝试将数据加载到导入器(由 Perch CMS 提供)以作为集合项导入。

问题是每个集合项都有相同的图像集。

看看我用来提取数据的 PHP 代码。

<?php
include('perch/runtime.php');
$images = array();
$location = array();

$API      = new PerchAPI(1.0, 'my_importer');
$Importer = $API->get('CollectionImporter');
$Importer->set_collection('property');
$Template = $API->get('Template');
$Template->set('content/text', 'content');
$Importer->set_template($Template);
$Importer->empty_collection();
$xml=simplexml_load_file("https://shades.mycrm.com/feed/privatesite/0744a0be5fac33c3b3e69c6b26e97d1e") or die("Error: Cannot create object");

foreach($xml->children() as $propertylisting) {    
foreach($propertylisting->photo->children() as $image_url) {
$images[] = $image_url->__toString(); 
}
    $location[] = $propertylisting->community->__toString(); 
    try {
    $Importer->add_item([
        'reference_number'   =>  $propertylisting->reference_number  ,
        'description_en'          =>  $propertylisting->description_en  ,
        'title_property'            =>  $propertylisting->title_en  ,
        'property_image_1'    =>  $images[0],
         'property_image_2'   =>  $images[1],
        'property_image_3'    =>  $images[2],
        'property_image_4'    =>  $images[3],
        'property_image_5'    =>  $images[4],
        'property_image_6'    =>  $images[5],
        
    ]);    
} catch (Exception $e) {
    die('Error: '.$e->getMessage());
}
} 
$result = array_unique($location);
?> 

我如何唯一地将图像添加到导入器中。

Nigel Ren

我可以看到发生的一个问题是,您永远不会在$images每次循环时清除数组,因此这意味着不是为此列表构建图像列表,而是为所有列表累积它们。如此简单地为每个属性列表重置数组...

foreach($xml->children() as $propertylisting) {    
    // Reset array
    $images = array();
    foreach($propertylisting->photo->children() as $image_url) {
        $images[] = $image_url->__toString(); 
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章