如何在php中解析复杂的xml

时间:2014-01-31 16:03:27

标签: php xml

我有一个xml文档,我正在尝试解析它(遍历项目),但似乎没有xml解析器理解它。我尝试过simpleXml和其他几个,但它们只提供两个字段(@attributes:locale,country)或不清楚的数组。使用online viewer我可以清楚地看到项目列表,因此它必须是php解析器的问题。    基本上我需要提取每个项目的图像,价格和位置。任何帮助在阵列中解码这将是非常感谢!  我基本上需要遍历“广告”数组并提取每个“广告”的信息。 id,价格,标题,开始日期时间,类别,位置,图片(数组)。这是一个打印屏幕:http://i.imgur.com/R9YkUZY.png。所以,如果没有10个声誉,我不会发布图片。

$xml =  simplexml_load_file('xml');
print_r($xml);

返回

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [locale] => en_GB
            [version] => 1.17
        )

)

我也尝试了一个我发现的课程(XML2Array)但没有运气!我正在考虑使用正则表达式,因为它是我的一个老朋友,但数据看起来似乎没有用正则表达式提取的结构。

$array = XML2Array::createArray($xml);
print_r($array);

下面是XML。我也把它贴在pastebin.com/XiKbfhV5上。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ad:ads xmlns:types="http://www.domainclassifiedsgroup.com/schema/types/v1" xmlns:cat="http://www.domainclassifiedsgroup.com/schema/category/v1" xmlns:loc="http://www.domainclassifiedsgroup.com/schema/location/v1" xmlns:ad="http://www.domainclassifiedsgroup.com/schema/ad/v1" xmlns:feat="http://www.domainclassifiedsgroup.com/schema/feature/v1" xmlns:attr="http://www.domainclassifiedsgroup.com/schema/attribute/v1" xmlns:pic="http://www.domainclassifiedsgroup.com/schema/picture/v1" xmlns:user="http://www.domainclassifiedsgroup.com/schema/user/v1" xmlns:rate="http://www.domainclassifiedsgroup.com/schema/rate/v1" xmlns:reply="http://www.domainclassifiedsgroup.com/schema/reply/v1" xmlns:feed="http://www.domainclassifiedsgroup.com/schema/feed/v1" xmlns:order="http://www.domainclassifiedsgroup.com/schema/order/v1" xmlns:payment="http://www.domainclassifiedsgroup.com/schema/payment/v1" locale="en_GB" version="1.17">
  <ad:ad id="1046911987">
    <ad:title>Wanted Apple Macbook pro - Air - Retina - iMac Call now Wanted Today</ad:title>
    <ad:ad-status>
      <ad:value>ACTIVE</ad:value>
    </ad:ad-status>
    <ad:start-date-time>2014-01-31T17:54:24.000Z</ad:start-date-time>
    <feat:features-active>
      <feat:feature-active display="true" name="AD_GP_TOP_AD" group="standard"/>
    </feat:features-active>
    <cat:category id="188">
      <cat:id-name>computing-phones-wanted</cat:id-name>
      <cat:localized-name>Computing &amp; Phones</cat:localized-name>
    </cat:category>
    <loc:locations>
      <loc:location id="124">
        <loc:id-name>wimbledon</loc:id-name>
        <loc:localized-name>Wimbledon</loc:localized-name>
      </loc:location>
      <loc:location id="10000392">
        <loc:id-name>uk</loc:id-name>
        <loc:localized-name>United Kingdom</loc:localized-name>
      </loc:location>
      <loc:location id="10000393">
        <loc:id-name>england</loc:id-name>
        <loc:localized-name>England</loc:localized-name>
      </loc:location>
      <loc:location id="375">
        <loc:id-name>south-west-london</loc:id-name>
        <loc:localized-name>South West London</loc:localized-name>
      </loc:location>
      <loc:location id="10000344">
        <loc:id-name>london</loc:id-name>
        <loc:localized-name>London</loc:localized-name>
      </loc:location>
    </loc:locations>
    <pic:pictures>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/MzE3WDU1MA==/z/I8wAAMXQVT9S6-LI/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/MzE3WDU1MA==/z/I8wAAMXQVT9S6-LI/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/MzE3WDU1MA==/z/I8wAAMXQVT9S6-LI/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/MzE3WDU1MA==/z/I8wAAMXQVT9S6-LI/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/MzE3WDU1MA==/z/I8wAAMXQVT9S6-LI/$_77.JPG"/>
      </pic:picture>
    </pic:pictures>
  </ad:ad>
  <ad:ad id="1046911555">
    <ad:price>
      <types:currency-iso-code>
        <types:value localized-label="£">GBP</types:value>
      </types:currency-iso-code>
      <types:amount>140</types:amount>
    </ad:price>
    <ad:title>Hillbilly Golf Trolley with batteries, Trolley Bag, Callaway Golf Clubs</ad:title>
    <ad:ad-status>
      <ad:value>ACTIVE</ad:value>
    </ad:ad-status>
    <ad:start-date-time>2014-01-31T17:50:21.000Z</ad:start-date-time>
    <feat:features-active>
      <feat:feature-active display="true" name="AD_GP_TOP_AD" group="standard"/>
    </feat:features-active>
    <cat:category id="160">
      <cat:id-name>golf-equipment</cat:id-name>
      <cat:localized-name>Golf Equipment</cat:localized-name>
    </cat:category>
    <loc:locations>
      <loc:location id="10000373">
        <loc:id-name>york</loc:id-name>
        <loc:localized-name>York</loc:localized-name>
      </loc:location>
      <loc:location id="11000029">
        <loc:id-name>north-yorkshire</loc:id-name>
        <loc:localized-name>North Yorkshire</loc:localized-name>
      </loc:location>
      <loc:location id="10000392">
        <loc:id-name>uk</loc:id-name>
        <loc:localized-name>United Kingdom</loc:localized-name>
      </loc:location>
      <loc:location id="10000393">
        <loc:id-name>england</loc:id-name>
        <loc:localized-name>England</loc:localized-name>
      </loc:location>
    </loc:locations>
    <pic:pictures>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/390AAOxygPtS695L/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/390AAOxygPtS695L/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/390AAOxygPtS695L/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/390AAOxygPtS695L/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/390AAOxygPtS695L/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/MTAyNFg3Njg=/z/eY8AAOxyUrZS695z/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/MTAyNFg3Njg=/z/eY8AAOxyUrZS695z/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/MTAyNFg3Njg=/z/eY8AAOxyUrZS695z/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/MTAyNFg3Njg=/z/eY8AAOxyUrZS695z/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/MTAyNFg3Njg=/z/eY8AAOxyUrZS695z/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/tKgAAMXQVERS696V/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/tKgAAMXQVERS696V/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/tKgAAMXQVERS696V/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/tKgAAMXQVERS696V/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NzY4WDEwMjQ=/z/tKgAAMXQVERS696V/$_77.JPG"/>
      </pic:picture>
    </pic:pictures>
  </ad:ad>
  <ad:ad id="1046912276">
    <ad:price>
      <types:currency-iso-code>
        <types:value localized-label="£">GBP</types:value>
      </types:currency-iso-code>
      <types:amount>130</types:amount>
    </ad:price>
    <ad:title>BEYONCE MRS CARTER SHOW**TOP PRICE SEATS* REDUCED PRICE!!</ad:title>
    <ad:ad-status>
      <ad:value>ACTIVE</ad:value>
    </ad:ad-status>
    <ad:start-date-time>2014-01-31T17:56:01.000Z</ad:start-date-time>
    <feat:features-active/>
    <cat:category id="84">
      <cat:id-name>pop-music-tickets</cat:id-name>
      <cat:localized-name>Pop</cat:localized-name>
    </cat:category>
    <loc:locations>
      <loc:location id="22010">
        <loc:id-name>birmingham-city-centre</loc:id-name>
        <loc:localized-name>Birmingham City Centre</loc:localized-name>
      </loc:location>
      <loc:location id="10000392">
        <loc:id-name>uk</loc:id-name>
        <loc:localized-name>United Kingdom</loc:localized-name>
      </loc:location>
      <loc:location id="10000393">
        <loc:id-name>england</loc:id-name>
        <loc:localized-name>England</loc:localized-name>
      </loc:location>
      <loc:location id="11000043">
        <loc:id-name>west-midlands</loc:id-name>
        <loc:localized-name>West Midlands</loc:localized-name>
      </loc:location>
      <loc:location id="10000347">
        <loc:id-name>birmingham</loc:id-name>
        <loc:localized-name>Birmingham</loc:localized-name>
      </loc:location>
    </loc:locations>
    <pic:pictures>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/ODAwWDQ1MQ==/z/OAIAAMXQVT9S6-Ow/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/ODAwWDQ1MQ==/z/OAIAAMXQVT9S6-Ow/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/ODAwWDQ1MQ==/z/OAIAAMXQVT9S6-Ow/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/ODAwWDQ1MQ==/z/OAIAAMXQVT9S6-Ow/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/ODAwWDQ1MQ==/z/OAIAAMXQVT9S6-Ow/$_77.JPG"/>
      </pic:picture>
    </pic:pictures>
  </ad:ad>
  <ad:ad id="1046912270">
    <ad:price>
      <types:currency-iso-code>
        <types:value localized-label="£">GBP</types:value>
      </types:currency-iso-code>
      <types:amount>0</types:amount>
    </ad:price>
    <ad:title>Brand New All Bedrooms Furniture - Best Quality &amp; Exclusive Offers</ad:title>
    <ad:ad-status>
      <ad:value>ACTIVE</ad:value>
    </ad:ad-status>
    <ad:start-date-time>2014-01-31T17:56:01.000Z</ad:start-date-time>
    <feat:features-active/>
    <cat:category id="681">
      <cat:id-name>other-bedroom-furniture</cat:id-name>
      <cat:localized-name>Other Bedroom Furniture &amp; Accs</cat:localized-name>
    </cat:category>
    <loc:locations>
      <loc:location id="10000344">
        <loc:id-name>london</loc:id-name>
        <loc:localized-name>London</loc:localized-name>
      </loc:location>
      <loc:location id="10000392">
        <loc:id-name>uk</loc:id-name>
        <loc:localized-name>United Kingdom</loc:localized-name>
      </loc:location>
      <loc:location id="10000393">
        <loc:id-name>england</loc:id-name>
        <loc:localized-name>England</loc:localized-name>
      </loc:location>
    </loc:locations>
    <pic:pictures>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/S84AAOxy63FS6-No/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/S84AAOxy63FS6-No/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/S84AAOxy63FS6-No/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/S84AAOxy63FS6-No/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/S84AAOxy63FS6-No/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/eHMAAOxyRhBS6-NK/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/eHMAAOxyRhBS6-NK/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/eHMAAOxyRhBS6-NK/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/eHMAAOxyRhBS6-NK/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/eHMAAOxyRhBS6-NK/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/VaIAAOxyOMdS6-NV/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/VaIAAOxyOMdS6-NV/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/VaIAAOxyOMdS6-NV/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/VaIAAOxyOMdS6-NV/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/VaIAAOxyOMdS6-NV/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/KdYAAOxyVaBS6-Na/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/KdYAAOxyVaBS6-Na/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/KdYAAOxyVaBS6-Na/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/KdYAAOxyVaBS6-Na/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/KdYAAOxyVaBS6-Na/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/mEwAAOxyOlhS6-Nh/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/mEwAAOxyOlhS6-Nh/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/mEwAAOxyOlhS6-Nh/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/mEwAAOxyOlhS6-Nh/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/mEwAAOxyOlhS6-Nh/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/Va0AAOxycmBS6-Ny/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/Va0AAOxycmBS6-Ny/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/Va0AAOxycmBS6-Ny/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/Va0AAOxycmBS6-Ny/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/Va0AAOxycmBS6-Ny/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/myoAAOxyOlhS6-N~/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/myoAAOxyOlhS6-N~/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/myoAAOxyOlhS6-N~/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/myoAAOxyOlhS6-N~/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/myoAAOxyOlhS6-N~/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/cLoAAOxyVLNS6-OE/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/cLoAAOxyVLNS6-OE/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/cLoAAOxyVLNS6-OE/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/cLoAAOxyVLNS6-OE/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NDUwWDYwMA==/z/cLoAAOxyVLNS6-OE/$_77.JPG"/>
      </pic:picture>
      <pic:picture>
        <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/MjgzWDQwMA==/z/V6EAAOxycmBS6-OJ/$_80.JPG"/>
        <pic:link rel="preview" href="http://i.domainimg.com/00/s/MjgzWDQwMA==/z/V6EAAOxycmBS6-OJ/$_81.JPG"/>
        <pic:link rel="big" href="http://i.domainimg.com/00/s/MjgzWDQwMA==/z/V6EAAOxycmBS6-OJ/$_79.JPG"/>
        <pic:link rel="thumb" href="http://i.domainimg.com/00/s/MjgzWDQwMA==/z/V6EAAOxycmBS6-OJ/$_78.JPG"/>
        <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/MjgzWDQwMA==/z/V6EAAOxycmBS6-OJ/$_77.JPG"/>
      </pic:picture>
    </pic:pictures>
  </ad:ad>
  <types:paging>
    <types:numFound>960522</types:numFound>
    <types:link rel="self" href="https://device-api.domainnone.com/api/ads?locationId=10000392&amp;categoryId=2549&amp;page=0&amp;size=2&amp;_ver=1.17"/>
    <types:link rel="next" href="https://device-api.domainnone.com/api/ads?locationId=10000392&amp;categoryId=2549&amp;page=1&amp;size=2&amp;_ver=1.17"/>
  </types:paging>
  <ad:ads-search-options>
    <ad:locationId>10000392</ad:locationId>
    <ad:categoryId>2549</ad:categoryId>
    <ad:sortType>
      <types:value>DATE_DESCENDING</types:value>
    </ad:sortType>
  </ad:ads-search-options>
  <ad:ads-search-histograms/>
</ad:ads>

如果你帮助我,我会让你出名!

2 个答案:

答案 0 :(得分:0)

通用转换不起作用。但是你可以使用Xpath从XML获取数据并构建自己的数据结构(数组,对象......),就像使用SQL和数据库中的数据一样。

我不使用SimpleXML,而是直接使用DOM(SimpleXML是DOM的包装器)。首先,您需要将XML加载到DOM文档中,为其创建一个Xpath对象,以及您的数据所在的命名空间。您可以独立于XML源定义自己的别名,但在示例中我将使用相同的。 / p>

$dom = new DOMDocument();
//$dom->load($xmlFile);
$dom->loadXml($xmlString);
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('ad', 'http://www.ebayclassifiedsgroup.com/schema/ad/v1');
$xpath->registerNamespace('cat', 'http://www.ebayclassifiedsgroup.com/schema/category/v1');

现在可以使用DOMXpath :: evaluate()从XML获取数据。根据表达式,结果将是节点列表或标量。像//element-name这样的表达式会返回一个列表,但是count(//element-name)一个数字。 evaluate()的第二个参数是上下文,如果你有命名空间,第三个参数应该总是FALSE。

$result = array();
$ads = $xpath->evaluate('/ad:ads/ad:ad', NULL, FALSE);
foreach ($ads as $ad) {
  $categories = [];
  foreach ($xpath->evaluate('cat:category', $ad, FALSE) as $category) {
    $categories[$category->getAttribute('id')] = [
      'name' => $xpath->evaluate('string(cat:id-name)', $category, FALSE),
      'title' => $xpath->evaluate('string(cat:localized-name)', $category, FALSE)
    ];
  }
  $result[] = [
    'id' => $ad->getAttribute('id'),
    'title' => $xpath->evaluate('string(ad:title)', $ad, FALSE),
    'categories' => $categories
  ];
}

SimpleXMLs xpath()方法的工作原理相同,但有一个很大区别 - 它只能返回列表,不能返回标量。

答案 1 :(得分:0)

您的解决方案对我不起作用

  error_reporting(E_ALL & ~E_NOTICE);
ini_set('track_errors', 1);
ini_set('display_errors', 1);

function CallAPI($method, $url, $data = false)
{
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // Optional Authentication:
    //curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    //curl_setopt($curl, CURLOPT_USERPWD, "username:password");

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);

    curl_close($curl);

    return $result;
}



$term="affitto";




$method='GET';
$url='http://rest.kijiji.it/columbus-api/open/ads';
$data= array('categoryId'=>'369295360','q'=>$term);
$xmlString=CallAPI($method, $url, $data);

$dom = new DOMDocument();
$dom->loadXml($xmlString);
var_dump($dom); 
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('ad', 'url');
$xpath->registerNamespace('cat', 'url');

$result = array();
$ads = $xpath->evaluate('/ad:ads/ad:ad', NULL, FALSE);
var_dump($dom);
foreach ($ads as $ad) {
  $categories = [];
  foreach ($xpath->evaluate('cat:category', $ad, FALSE) as $category) {
    $categories[$category->getAttribute('id')] = [
      'name' => $xpath->evaluate('string(cat:id-name)', $category, FALSE),
      'title' => $xpath->evaluate('string(cat:localized-name)', $category, FALSE)
    ];
  }
  $result[] = [
    'id' => $ad->getAttribute('id'),
    'title' => $xpath->evaluate('string(ad:title)', $ad, FALSE),
    'categories' => $categories
  ];
}

var_dump($result);