我正在尝试解析评级服务的UPS示例代码,当我收到UPS的回复时,我似乎无法用它做任何事情。我如何阅读并找到我想要的数据?例如
<rate:RatedShipment><rate:Service><rate:Code>03</rate:Code>
和
<rate:TotalCharges>
<rate:CurrencyCode>USD</rate:CurrencyCode>
<rate:MonetaryValue>126.72</rate:MonetaryValue>
</rate:TotalCharges>
我需要那些代码和总费用。 到目前为止我的代码看起来像这样:
$result = $client->__soapCall($operation ,array($this->processRate()));
$resp = $client->__getLastResponse() ;
echo $resp ;
输出:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header />
<soapenv:Body>
<rate:RateResponse xmlns:rate="http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1">
<common:Response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
<common:ResponseStatus>
<common:Code>1</common:Code>
<common:Description>Success</common:Description>
</common:ResponseStatus>
<common:Alert>
<common:Code>110971</common:Code>
<common:Description>Your invoice may vary from the displayed reference rates</common:Description>
</common:Alert>
<common:Alert>
<common:Code>110920</common:Code>
<common:Description>Ship To Address Classification is changed from Residential to Commercial</common:Description>
</common:Alert>
<common:TransactionReference />
</common:Response>
<rate:RatedShipment>
<rate:Service>
<rate:Code>03</rate:Code>
<rate:Description />
</rate:Service>
<rate:RatedShipmentAlert>
<rate:Code>110971</rate:Code>
<rate:Description>Your invoice may vary from the displayed reference rates</rate:Description>
</rate:RatedShipmentAlert>
<rate:RatedShipmentAlert>
<rate:Code>110920</rate:Code>
<rate:Description>Ship To Address Classification is changed from Residential to Commercial</rate:Description>
</rate:RatedShipmentAlert>
<rate:BillingWeight>
<rate:UnitOfMeasurement>
<rate:Code>LBS</rate:Code>
<rate:Description>Pounds</rate:Description>
</rate:UnitOfMeasurement>
<rate:Weight>3.0</rate:Weight>
</rate:BillingWeight>
<rate:TransportationCharges>
<rate:CurrencyCode>USD</rate:CurrencyCode>
<rate:MonetaryValue>17.76</rate:MonetaryValue>
</rate:TransportationCharges>
<rate:ServiceOptionsCharges>
<rate:CurrencyCode>USD</rate:CurrencyCode>
<rate:MonetaryValue>0.00</rate:MonetaryValue>
</rate:ServiceOptionsCharges>
<rate:TotalCharges>
<rate:CurrencyCode>USD</rate:CurrencyCode>
<rate:MonetaryValue>17.76</rate:MonetaryValue>
</rate:TotalCharges>
</rate:RatedShipment>
......
</rate:RateResponse>
</soapenv:Body>
</soapenv:Envelope>
然后我在其上运行此代码:
$xml = simplexml_load_string($resp);
\TYPO3\Flow\var_dump($xml );
$json = json_encode($xml);
$xmlarray = json_decode($json,TRUE);
echo "\n".__FILE__.' '.__LINE__." xmlarray \n";
\TYPO3\Flow\var_dump($xmlarray);
die;
此输出
<div class="Flow-Error-Debugger-VarDump Flow-Error-Debugger-VarDump-Floating">
<div class="Flow-Error-Debugger-VarDump-Top">
Flow Variable Dump
</div>
<div class="Flow-Error-Debugger-VarDump-Center">
<pre dir="ltr"><span class="debug-object debug-unregistered" title="00000000366333b30000000074e4ee7f">SimpleXMLElement</span><span class="debug-scope">prototype<a id="o00000000366333b30000000074e4ee7f"></a></span><span class="debug-ptype" title="unknown">object</span></pre>
</div>
</div>
/home/me/domains/shop.me.com/public_html/releases/20131219160416/Data/Temporary/Development/Cache/Code/Flow_Object_Classes/Shop_Shipping_UPSShippingHandler.php 336 xmlarray
<div class="Flow-Error-Debugger-VarDump Flow-Error-Debugger-VarDump-Floating">
<div class="Flow-Error-Debugger-VarDump-Top">
Flow Variable Dump
</div>
<div class="Flow-Error-Debugger-VarDump-Center">
<pre dir="ltr">array(empty)</pre>
</div>
</div>
似乎我的simplexml_load_string($resp)
电话无效。
更新: 谢谢外星人。这就是我的代码现在的样子 - 它还不完美,但你明白了 - :
$result = $client->__soapCall($operation ,array($this->processRate()));
$resp = $client->__getLastResponse() ;
$sxe = simplexml_load_string($resp);
$sxe->registerXPathNamespace('r', "http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1");
$codes = $sxe->xpath('//r:RatedShipment/r:Service/r:Code');
$mv = $sxe->xpath('//r:TotalCharges/r:MonetaryValue');
$indx=0;
$estimates = array();
foreach ($codes as $c) {
$temporaryEstimate = array();
$temporaryEstimate['estimateId'] = $this->getServiceTitleByCode($c->__toString());
$temporaryEstimate['estimatePrice'] = '$' .$mv[$indx];
$estimates[] = $temporaryEstimate;
$indx++;
}
return $estimates;
答案 0 :(得分:3)
没有必要转换为JSON并再次返回(除非你喜欢让你的计算机工作!);我将您发布的XML直接加载到SimpleXMLElement中,然后使用XPath查询它:
$sxe = new SimpleXMLElement($resp);
您感兴趣的元素都在rate
命名空间中,因此您需要注册命名空间才能查询它:
$sxe->registerXPathNamespace('r', "http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1");
我们现在可以使用简写rate:
来引用XML中的所有r:
个节点。
查询Code
命名空间中RatedShipment/Service
下的所有r
元素:
$codes = $sxe->xpath('//r:RatedShipment/r:Service/r:Code');
foreach ($codes as $c) {
echo "code: $c" . PHP_EOL;
}
输出:
code: 03
查看货件的总费用,该费用低于r:TotalCharges
r:MonetaryValue
和r:CurrencyCode
:
# xpath returns an array; we want the first element of that array for both these
$curr = $sxe->xpath('//r:TotalCharges/r:CurrencyCode')[0];
$mv = $sxe->xpath('//r:TotalCharges/r:MonetaryValue')[0];
echo "total charges: $mv $curr ". PHP_EOL;
输出:
total charges: 17.76 USD
如果您不熟悉XPath,请参阅PHP文档以获取有关SimpleXML's XPath implementation的更多信息。