Domanda

I've been looking for days for the correct way to read the XML that UPS API returns to me. I finally found how to make a petition for a rate from a package to send, and now I got the XML with the response.

I'm not very familiar with XML, but I can understand how it works with simple examples.

The XML response is:

<?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>

I tried an example to get the values with simplexml_load_file(); and I could get the values from a tag (a very simple example). But when I try it with that one, I can't get anything, because it says an error of beign non an object-type.

I'd be very grateful if someone knows how to read that XML and teach me how to do it.

Thanks for your time reading this!

P.S: When I tried a simple example, I tried this and worked:

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

This worked properly, but when I'm trying to get, for example, Response->RatedShipment[0]->Service->Code; */to get the first one/*, the error appears.

È stato utile?

Soluzione

Why don't you try the SoapClient interface??

$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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top