2013-03-19 42 views
2

我一直在尋找正確的方法來讀取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] - >服務 - >碼; */以獲得第一個/*,則會顯示錯誤。

+1

您可以發佈[代碼](http://sscce.org/)? – phihag 2013-03-19 11:30:23

+0

我在最後添加了我用於該示例的php,該工作正常,並且爲我的UPS XML – Ardilla 2013-03-19 11:37:11

回答

4

你爲什麼不嘗試SoapClient接口?

$client = new SoapClient('http://host/api/soap/?wsdl'); 

// If somestuff requires api authentification, 
// then get a session token 
$session = $client->login('apiUser', 'apiKey'); 

http://php.net/manual/en/class.soapclient.php

+0

嘗試了幾乎相同的感謝,因爲我們終於得到了一個配偶(我想)我們需要感謝您。在製作XML的末尾,有一部分代碼使用了soapCall方法,並且這正是我們所需要的。 – Ardilla 2013-03-19 12:13:11

+0

不客氣。我知道它有多令人沮喪;-)我試圖找出洋紅皁...... *恐怖*。格拉斯我可以幫忙。 – Tschallacka 2013-03-19 14:52:57