这里可以请求
$data1 ='<?xml version="1.0" encoding="utf-8" ?>
<IDV>
<policy_start_date>21/12/2017</policy_start_date>
<vehicle_class_cd>45</vehicle_class_cd>
<RTOLocationCode>10416</RTOLocationCode>
<vehiclemodelcode>12474</vehiclemodelcode>
<manufacturer_code>27</manufacturer_code>
<purchaseregndate>21/12/2017</purchaseregndate>
<prev_policy_end_date>21/12/2016</prev_policy_end_date>
<manufacturingyear>2014</manufacturingyear>
</IDV>';
$url = "http://202.191.196.210/uat/onlineproducts/wscalculate/service.asmx/getIDV";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,"str=" . $data1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
在echo中我得到了以下回复
<IDV>
<exshowroomPrice>710570</exshowroomPrice>
<idv_amount>675042</idv_amount>
<idv_amount_min>607537</idv_amount_min>
<idv_amount_max>776298</idv_amount_max>
<exshowroomPrice_min>639513</exshowroomPrice_min>
<exshowroomPrice_max>817156</exshowroomPrice_max>
<outputmessage></outputmessage>
</IDV>
现在如果我用
$xml = simplexml_load_string($response);
print_r($xml);
我收到了以下回复
SimpleXMLElement Object ( [0] => 710570675042607537776298639513817156 )
我想提取
<exshowroomPrice>710570</exshowroomPrice>
如何提取那一个。请帮助我,我尝试了很多解决方案,但它没有工作。
答案 0 :(得分:1)
只需使用SimpleXMLElement,
$xmlString =
'<IDV>
<exshowroomPrice>710570</exshowroomPrice>
<idv_amount>675042</idv_amount>
<idv_amount_min>607537</idv_amount_min>
<idv_amount_max>776298</idv_amount_max>
<exshowroomPrice_min>639513</exshowroomPrice_min>
<exshowroomPrice_max>817156</exshowroomPrice_max>
<outputmessage></outputmessage>
</IDV>';
$xml = new SimpleXMLElement($xmlString);
echo $xml->exshowroomPrice;
答案 1 :(得分:0)
如果使用PHP函数simplexml_load_string
正确创建对象 $response = "
<IDV>
<exshowroomPrice>710570</exshowroomPrice>
<idv_amount>675042</idv_amount>
<idv_amount_min>607537</idv_amount_min>
<idv_amount_max>776298</idv_amount_max>
<exshowroomPrice_min>639513</exshowroomPrice_min>
<exshowroomPrice_max>817156</exshowroomPrice_max>
<outputmessage></outputmessage>
</IDV>";
$xml = simplexml_load_string($response);
然后创建的对象看起来像print_r($xml);
SimpleXMLElement Object
(
[exshowroomPrice] => 710570
[idv_amount] => 675042
[idv_amount_min] => 607537
[idv_amount_max] => 776298
[exshowroomPrice_min] => 639513
[exshowroomPrice_max] => 817156
[outputmessage] => SimpleXMLElement Object
(
)
)
所以你只需要调用/ echo你需要的属性:
echo $xml->exshowroomPrice;
echo '<exshowroomPrice>' . $xml->exshowroomPrice . '</exshowroomPrice>';
将打印:
710570
<exshowroomPrice>710570</exshowroomPrice>
<强> Live code on eval.in 强>
答案 2 :(得分:0)
首先你必须使用下面的代码检查xml中输出的格式是否正确,如果没有错误,则simplexml_load_string - 将XML字符串解释为对象。
libxml_use_internal_errors(true);
$doc = simplexml_load_string($response);
$xml = explode("\n", $response);
if (!$doc) {
$errors = libxml_get_errors();
foreach ($errors as $error) {
echo display_xml_error($error, $xml);
}
libxml_clear_errors();
} else {
$note = <<<XML $response XML;
$xml = simplexml_load_string($note);
$xml = (array) $xml;
echo $xml['exshowroomPrice'];
print_r($xml);
}
答案 3 :(得分:0)
你回来的XML看起来有点奇怪,但这并不罕见。它似乎将主要内容放在引号中,这并不重要,但它看起来很奇怪。更糟糕的是,它尝试使用的名称空间应该更像是一个正确的网址 - tempuri.org/
应该是http://tempuri.org/
或类似的名称。
无论如何 - 以下工作原理(尽管根据命名空间显示一些警告是相对的)。主要的是<string>
元素具有默认命名空间,因此在加载数据时,需要考虑这一点。所以我所做的就是一旦我加载了它,我就会使用这个命名空间让孩子们(<IDV>
等)。然后可以正常访问生成的SimpleXMLElement。
$response ='<string xmlns="tempuri.org/">; "
<IDV>
<exshowroomPrice>710570</exshowroomPrice>
<idv_amount>675042</idv_amount>
<idv_amount_min>607537</idv_amount_min>
<idv_amount_max>776298</idv_amount_max>
<exshowroomPrice_min>639513</exshowroomPrice_min>
<exshowroomPrice_max>817156</exshowroomPrice_max>
<outputmessage></outputmessage>
</IDV>"
</string>';
$xml = simplexml_load_string($response, SimpleXMLElement::class,
LIBXML_NOERROR );
$idv = $xml->children("tempuri.org/");
echo "exshowroomPrice ".$idv->IDV->exshowroomPrice.PHP_EOL;
其输出是......
exshowroomPrice 710570
更新:
由于你回来的数据有标记编码(即它返回为<IDV>
),你需要在将它传递给SimpleXML之前对其进行解码......
$response = html_entity_decode((string)$response);
$xml = simplexml_load_string($response, SimpleXMLElement::class,
LIBXML_NOERROR );
$idv = $xml->children("http://tempuri.org/");
echo "exshowroomPrice ".$idv->IDV->exshowroomPrice.PHP_EOL;