下面的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;
}
?>
请问我做错了什么? 非常感谢
答案 0 :(得分: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 />";
}