2

我一直在寻找正确的方法来读取 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();并且可以从标签中获取值(一个非常简单的示例)。但是当我用那个尝试它时,我什么也得不到,因为它说一个非对象类型的错误。

如果有人知道如何阅读该 XML 并教我如何去做,我将不胜感激。

感谢您花时间阅读本文!

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

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

这工作正常,但是当我试图获得例如 Response->RatedShipment[0]->Service->Code; */获取第一个/*,出现错误。

4

1 回答 1

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

于 2013-03-19T11:38:04.920 回答