使用XML数据作为数组

时间:2014-03-31 09:29:26

标签: php xml curl soap asmx

我遇到了问题,我正试图从http://www.acquaintcrm.co.uk/propertyfunctions/propertysearch.asmx?op=GetDataV8

获取数据

我已经发出请求并将数据输出到文件properties.xml

该文件充满了数据,但我只是不知道如何使用该数据,我想将xml数据转换为数组,但老实说我不知道​​从哪里开始。

我的代码

$xml_data = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <GetDataV8 xmlns="http://www.AcquaintCRM.co.uk/propertyfunctions/">
          <strSitePrefix>STDZ</strSitePrefix>
          <intSiteID>-1</intSiteID>
          <intPropertyID>0</intPropertyID>
          <intPropertyDevelopmentID>0</intPropertyDevelopmentID>
          <bytBedrooms>0</bytBedrooms>
          <decMinPrice>0</decMinPrice>
          <decMaxPrice>0</decMaxPrice>
          <bytTenure>1</bytTenure>
          <intCommercial>-1</intCommercial>
          <bytPropertyAge>0</bytPropertyAge>
          <intRentalTerms>0</intRentalTerms>
          <bytFeaturedCount>0</bytFeaturedCount>
          <strAreas></strAreas>
          <strTypes></strTypes>
          <bytSortOrder>0</bytSortOrder>
          <bytUseCDataTags>0</bytUseCDataTags>
        </GetDataV8>
      </soap:Body>
    </soap:Envelope>
';

$url = "http://www.acquaintcrm.co.uk/propertyfunctions/propertysearch.asmx?op=GetDataV8";

$headers  = array(
  "POST * HTTP/1.1",
  "Content-type: text/xml;charset=\"utf-8\"",
  "Accept: text/xml",
  "Cache-Control: no-cache",
  "Pragma: no-cache",
  "Content-length: ".strlen($xml_data),
  "SOAPAction: http://www.AcquaintCRM.co.uk/propertyfunctions/GetDataV8",
);

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

$response = curl_exec($ch);

if (curl_errno($ch))
{
    echo 'Error: ';
    echo curl_error($ch);
}
else
{
  curl_close($ch);

  $xml = file_put_contents('properties.xml', $response);

  $xml = simplexml_load_file('properties.xml');

  print_r($xml);

}

可以在此处查看XML文件

http://manchester.studentdigz.com/properties.xml

提前谢谢大家,感谢所有人的帮助!

1 个答案:

答案 0 :(得分:1)

您可以使用php simplexml(http://php.net/manual/en/book.simplexml.php)在对象中加载字符串。

请注意,如果你真的想要一个数组,你也可以这样做:

$xml = simplexml_load_string($xmlstring)->children('http://schemas.xmlsoap.org/soap/envelope/')->children('Body');
$json = json_encode($xml);
$array = json_decode($json,TRUE);

echo "<pre>";
echo "xml object : <br/>"; 
var_dump($xml);
echo "<hr/>";
echo "json string : <br/>"; 
var_dump($json);
echo "<hr/>";
echo "array : <br/>"; 
var_dump($array);