0

下面的 URL 链接返回一个带有单个节点的 XML:

http://national.atdw.com.au/soap/AustralianTourismWebService.asmx/CommandHandler?DistributorKey=201201100935&CommandName=QueryProducts&CommandParameters=<parameters>
<row><param>PRODUCT_CATEGORY_LIST</param><value>ACCOMM</value></row>
</parameters>

我打算遍历响应并提取一些属性,例如(product_name,product_description)。当我运行程序时,我的 php 代码没有返回任何值。请看下面的代码:

<?php

$url = file_get_contents("http://national.atdw.com.au/soap/AustralianTourismWebService.asmx/CommandHandler?DistributorKey=201201100935&CommandName=QueryProducts&CommandParameters=<parameters>
<row><param>PRODUCT_CATEGORY_LIST</param><value>ACCOMM</value></row>
</parameters>");

$xml = simplexml_load_string($url);

foreach ($xml->item as $entry) {
echo $entry->product_name;
echo $entry->product_description;
}

?>

请问我做错了什么?非常感谢

4

1 回答 1

0

您从服务中获得的 XML 无效。事实上,你可以让它有效,但它会使你的脚本运行得更慢。但如果你别无选择,你可以

$url = file_get_contents("http://national.atdw.com.au/soap/AustralianTourismWebService.asmx/CommandHandler?DistributorKey=201201100935&CommandName=QueryProducts&CommandParameters=<parameters><row><param>PRODUCT_CATEGORY_LIST</param><value>ACCOMM</value></row></parameters>");

$url = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $url);
$url = str_replace('<string xmlns="http://tempuri.org/soap/AustralianTourismWebService">', '', $url);
$url = str_replace('</string>', '', $url);
$url = str_replace('utf-16', 'utf-8', $url);

$xml = simplexml_load_string(trim(html_entity_decode($url)), 'SimpleXMLElement');

foreach ($xml->product_distribution as $entry) {
    echo "Product name: " . $entry->product_record->product_name . "<br />";
    echo "Product description:" . $entry->product_record->product_description . "<br />";
}
于 2013-10-09T16:16:34.363 回答