php - 如何从 HTML 中的 curl 返回 XML?
问题描述
<?php
$soapUrl = "http://10.65.28.221/Magic94Scripts/mgrqispi94.dll";
$xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:car="CarproPriceEstimation">
<soapenv:Header/>
<soapenv:Body>
<car:EstimationDetails UserId="TEST" UserPassword="TEST">
<!--1 or more repetitions:-->
<car:Price>
<car:CDP></car:CDP>
<car:OutBranch>'.$_SESSION["pickup-location"].'</car:OutBranch>
<car:InBranch>'.$_SESSION["dropLocation"].'</car:InBranch>
<car:OutDate>'.$_SESSION["pickupDate"].'</car:OutDate>
<car:OutTime>'.$_SESSION["pickupTime"].'</car:OutTime>
<car:InDate>'.$_SESSION["dropDate"].'</car:InDate>
<car:InTime>'.$_SESSION["dropTime"].'</car:InTime>
<car:CarGroup>'.$_SESSION["car-type"].'</car:CarGroup>
</car:Price>
</car:EstimationDetails>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: 3575E6F6964716D6964737545636962705F237375636F62705024727F607D694A336F646",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec($ch);
curl_close($ch);
// $xml= (array) simplexml_load_string($result);
// var_dump($xml);
$xml = simplexml_load_string($result, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
var_dump($array);
//
// $xml = simplexml_load_string($result);
// print_r($xml);
// // $json = json_encode($xml);
// // $ctarray = json_decode($json,TRUE);
// echo $ctarray;
?>
我正在尝试通过转换为数组来获取我的 html 中的 xml 返回数据,或者无论如何我在同一页面上有 html 想要从 xml 在 HTML 的不同位置获取一些值。我已经预计来自 API 的一些汽车价格的日期,我将在同一页面上显示,我没有使用 ajax,只是重定向到 curl 将运行的页面,并将在 html 页面的底部显示数据。
当我回显结果时,我得到了这个 XML。
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:body><estimationdetails>
<price>
<debitorcode></debitorcode>
<cdp></cdp>
<vouchertype></vouchertype>
<vouchercode></vouchercode>
<cargroup>A</cargroup>
<currency></currency>
<outbranch>66</outbranch>
<inbranch>66</inbranch>
<outdate>01/03/2020</outdate>
<outtime>06:30</outtime>
<indate>01/04/2020</indate>
<intime>02:30</intime>
<status>Status : Confirmed</status>
<cargroupprice cargroup="A">
<rateno>0</rateno>
<ratename></ratename>
<solddays>31.00</solddays>
<rentalsum>0.0000</rentalsum>
<insurancesum>0.0000</insurancesum>
<extrassum>0.0000</extrassum>
<dropoffsum>0.0000</dropoffsum>
<airportfee>0.0000</airportfee>
<vatperc>5.00</vatperc>
<vatamount>0.0000</vatamount>
<totalamount>0.0000</totalamount>
<alert></alert>
<caravailable>Available</caravailable>
<tariffcode>DEFAULT</tariffcode>
</cargroupprice>
</price>
<success>Y</success>
</estimationdetails></soap-env:body></soap-env:envelope>
解决方案
推荐阅读
- c# - NLog - 为什么在调用 Shutdown 后不释放锁定的文件?
- c - getchar 与 scanf:为什么 getchar 不起作用?
- c++ - 如何将复数传递给模板化数组搜索函数?
- laravel - 如何使用 Laravel 在同一个模型中编写两个表?
- reactjs - 我可以有效地使用 React 的 useCallback 和嵌套函数提升吗?
- python - CronTab 一年只运行一次
- pandas - 使用 python 和 tkinter 从 MySQL 导入数据
- node.js - 用非英文字符填写 pdf 表格
- python - Django - 无法使用信号删除对象更改的旧文件
- mono - Mono.switchIfEmpty with Mono.then evaluation order