用PHP读取UPS XML

时间:2013-03-19 11:16:27

标签: php xml api simplexml ups

我一直在寻找正确的方法来阅读UPS API返回给我的XML。我终于找到了如何根据包发送请求,现在我得到了带响应的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();的值,我可以从标记中获取值(一个非常简单的示例)。但是当我尝试使用那个时,我无法得到任何东西,因为它表示beign的错误而不是对象类型。

如果有人知道如何阅读XML并告诉我如何操作,我将非常感激。

感谢您抽出时间阅读本文!

P.S: 当我尝试一个简单的例子时,我尝试了这个并且工作了:

$school = simplexml_load_file('XOLTResult.xml'); //where is the xml
echo $school->students->student[0]; //finding the first student nam

这个工作正常,但是当我想要获取时,例如,Response-&gt; RatedShipment [0] - &gt; Service-&gt; Code; * / 获取第一个 / *,出现错误。

1 个答案:

答案 0 :(得分: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