2013-03-19 81 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