我一直在尋找正確的方法來讀取UPS API返回給我的XML的日子。我終於找到了如何從一個包發送請求,現在我得到了XML與響應。用PHP讀取UPS XML
我對XML並不是很熟悉,但我可以通過簡單的例子瞭解它是如何工作的。
XML響應是:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<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:TransactionReference/>
</common:Response>
<rate:RatedShipment>
<rate:Service>
<rate:Code>11</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:BillingWeight>
<rate:UnitOfMeasurement>
<rate:Code>KGS</rate:Code>
<rate:Description>Kilograms</rate:Description>
</rate:UnitOfMeasurement>
<rate:Weight>3.0</rate:Weight>
</rate:BillingWeight>
<rate:TransportationCharges>
<rate:CurrencyCode>EUR</rate:CurrencyCode>
<rate:MonetaryValue>21.85</rate:MonetaryValue>
</rate:TransportationCharges>
<rate:ServiceOptionsCharges>
<rate:CurrencyCode>EUR</rate:CurrencyCode>
<rate:MonetaryValue>1.40</rate:MonetaryValue>
</rate:ServiceOptionsCharges>
<rate:TotalCharges>
<rate:CurrencyCode>EUR</rate:CurrencyCode>
<rate:MonetaryValue>23.25</rate:MonetaryValue>
</rate:TotalCharges>
<rate:RatedPackage>
<rate:Weight>1.0</rate:Weight>
</rate:RatedPackage>
<rate:RatedPackage>
<rate:Weight>2.0</rate:Weight>
</rate:RatedPackage>
</rate:RatedShipment>
</rate:RateResponse>
</soapenv:Body>
</soapenv:Envelope>
我試過一個例子與simplexml_load_file();
得到的值,我可以從標籤(一個很簡單的例子)得到的值。但是當我嘗試使用它時,我無法得到任何東西,因爲它說明了一個對象類型的錯誤。
如果有人知道如何讀取該XML並教會我如何去做,我將不勝感激。
感謝您的閱讀時間!
PS: 當我試圖一個簡單的例子,我想這和工作:
$school = simplexml_load_file('XOLTResult.xml'); //where is the xml
echo $school->students->student[0]; //finding the first student nam
這工作正常,但是當我試圖得到的,例如,響應 - > RatedShipment [0] - >服務 - >碼; */以獲得第一個/*,則會顯示錯誤。
您可以發佈[代碼](http://sscce.org/)? – phihag 2013-03-19 11:30:23
我在最後添加了我用於該示例的php,該工作正常,並且爲我的UPS XML – Ardilla 2013-03-19 11:37:11