1

I've successfully implemented the UPS tracking API but the response is a complex xml file and I only need basic data from it. I need the status descriptions, dates and times and the delivery address.

<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <trk:TrackResponse
            xmlns:trk="http://www.ups.com/XMLSchema/XOLTWS/Track/v2.0">
            <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:TransactionReference>
                    <common:CustomerContext>track order</common:CustomerContext>
                </common:TransactionReference>
            </common:Response>
            <trk:Shipment>
                <trk:InquiryNumber>
                    <trk:Code>01</trk:Code>
                    <trk:Description>ShipmentIdentificationNumber</trk:Description>
                    <trk:Value>1ZA474A76843297635</trk:Value>
                </trk:InquiryNumber>
                <trk:ShipperNumber>A474A7</trk:ShipperNumber>
                <trk:ShipmentAddress>
                    <trk:Type>
                        <trk:Code>01</trk:Code>
                        <trk:Description>Shipper Address</trk:Description>
                    </trk:Type>
                    <trk:Address>
                        <trk:AddressLine>GRANGEFIELD IND EST</trk:AddressLine>
                        <trk:City>LEEDS</trk:City>
                        <trk:PostalCode>LS28 6QW</trk:PostalCode>
                        <trk:CountryCode>GB</trk:CountryCode>
                    </trk:Address>
                </trk:ShipmentAddress>
                <trk:ShipmentAddress>
                    <trk:Type>
                        <trk:Code>02</trk:Code>
                        <trk:Description>ShipTo Address</trk:Description>
                    </trk:Type>
                    <trk:Address>
                        <trk:AddressLine>75086</trk:AddressLine>
                        <trk:AddressLine>ROCKLINE INDUSTRIES LTD</trk:AddressLine>
                        <trk:AddressLine>HEMING ROAD</trk:AddressLine>
                        <trk:City>REDDITCH</trk:City>
                        <trk:PostalCode>B98 0DH</trk:PostalCode>
                        <trk:CountryCode>GB</trk:CountryCode>
                    </trk:Address>
                </trk:ShipmentAddress>
                <trk:ShipmentWeight>
                    <trk:UnitOfMeasurement>
                        <trk:Code>KGS</trk:Code>
                    </trk:UnitOfMeasurement>
                    <trk:Weight>1.00</trk:Weight>
                </trk:ShipmentWeight>
                <trk:Service>
                    <trk:Code>011</trk:Code>
                    <trk:Description>UPS Standard</trk:Description>
                </trk:Service>
                <trk:ReferenceNumber>
                    <trk:Code>16</trk:Code>
                    <trk:Value>757142/1</trk:Value>
                </trk:ReferenceNumber>
                <trk:ReferenceNumber>
                    <trk:Code>17</trk:Code>
                    <trk:Value>75086</trk:Value>
                </trk:ReferenceNumber>
                <trk:PickupDate>20230118</trk:PickupDate>
                <trk:Package>
                    <trk:TrackingNumber>1ZA474A76843297635</trk:TrackingNumber>
                    <trk:PackageServiceOption>
                        <trk:Type>
                            <trk:Code>030</trk:Code>
                            <trk:Description>Hazardous Materials</trk:Description>
                        </trk:Type>
                    </trk:PackageServiceOption>
                    <trk:Activity>
                        <trk:ActivityLocation>
                            <trk:Address>
                                <trk:AddressLine>THE WASHFORD INDUSTRIAL EST</trk:AddressLine>
                                <trk:City>REDDITCH</trk:City>
                                <trk:PostalCode>B98 0DH</trk:PostalCode>
                                <trk:CountryCode>GB</trk:CountryCode>
                            </trk:Address>
                            <trk:Code>M4</trk:Code>
                            <trk:Description>Reception</trk:Description>
                            <trk:SignedForByName>DAWN</trk:SignedForByName>
                        </trk:ActivityLocation>
                        <trk:Status>
                            <trk:Type>D</trk:Type>
                            <trk:Description>Delivered</trk:Description>
                            <trk:Code>9E</trk:Code>
                        </trk:Status>
                        <trk:Date>20230119</trk:Date>
                        <trk:Time>095320</trk:Time>
                        <trk:Document>
                            <trk:Type>
                                <trk:Code>04</trk:Code>
                                <trk:Description>POD Letter</trk:Description>
                            </trk:Type>
                            <trk:Content>PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwvL0VOIj48SFRNTD48SEVBRD48TUVUQSBIVFRQLUVRVUlWPSJDb250ZW50LVR5cGUiIENPTlRFTlQ9InRleHQvaHRtbDsgY2hhcnNldD1JU08tODg1OS0xIj48VElUTE0RZPjwvSFRNTD4=</trk:Content>
                            <trk:Format>
                                <trk:Code>03</trk:Code>
                                <trk:Description>HTML</trk:Description>
                            </trk:Format>
                        </trk:Document>
                    </trk:Activity>
                    <trk:Activity>
                        <trk:ActivityLocation>
                            <trk:Address>
                                <trk:City>Tamworth</trk:City>
                                <trk:CountryCode>GB</trk:CountryCode>
                            </trk:Address>
                            <trk:Description>Reception</trk:Description>
                        </trk:ActivityLocation>
                        <trk:Status>
                            <trk:Type>I</trk:Type>
                            <trk:Description>Out For Delivery</trk:Description>
                            <trk:Code>OF</trk:Code>
                        </trk:Status>
                        <trk:Date>20230119</trk:Date>
                        <trk:Time>052200</trk:Time>
                    </trk:Activity>
                    <trk:Activity>
                        <trk:ActivityLocation>
                            <trk:Address>
                                <trk:City>Tamworth</trk:City>
                                <trk:CountryCode>GB</trk:CountryCode>
                            </trk:Address>
                            <trk:Description>Reception</trk:Description>
                        </trk:ActivityLocation>
                        <trk:Status>
                            <trk:Type>I</trk:Type>
                            <trk:Description>Arrived at Facility</trk:Description>
                            <trk:Code>AR</trk:Code>
                        </trk:Status>
                        <trk:Date>20230119</trk:Date>
                        <trk:Time>013700</trk:Time>
                    </trk:Activity>
                    <trk:Activity>
                        <trk:ActivityLocation>
                            <trk:Address>
                                <trk:City>Preston</trk:City>
                                <trk:CountryCode>GB</trk:CountryCode>
                            </trk:Address>
                            <trk:Description>Reception</trk:Description>
                        </trk:ActivityLocation>
                        <trk:Status>
                            <trk:Type>I</trk:Type>
                            <trk:Description>Departed from Facility</trk:Description>
                            <trk:Code>DP</trk:Code>
                        </trk:Status>
                        <trk:Date>20230118</trk:Date>
                        <trk:Time>233000</trk:Time>
                    </trk:Activity>
                    <trk:Activity>
                        <trk:ActivityLocation>
                            <trk:Address>
                                <trk:City>Preston</trk:City>
                                <trk:CountryCode>GB</trk:CountryCode>
                            </trk:Address>
                            <trk:Description>Reception</trk:Description>
                        </trk:ActivityLocation>
                        <trk:Status>
                            <trk:Type>I</trk:Type>
                            <trk:Description>Arrived at Facility</trk:Description>
                            <trk:Code>AR</trk:Code>
                        </trk:Status>
                        <trk:Date>20230118</trk:Date>
                        <trk:Time>214500</trk:Time>
                    </trk:Activity>
                    <trk:Activity>
                        <trk:ActivityLocation>
                            <trk:Address>
                                <trk:City>Leeds</trk:City>
                                <trk:CountryCode>GB</trk:CountryCode>
                            </trk:Address>
                            <trk:Description>Reception</trk:Description>
                        </trk:ActivityLocation>
                        <trk:Status>
                            <trk:Type>I</trk:Type>
                            <trk:Description>Departed from Facility</trk:Description>
                            <trk:Code>DP</trk:Code>
                        </trk:Status>
                        <trk:Date>20230118</trk:Date>
                        <trk:Time>204000</trk:Time>
                    </trk:Activity>
                    <trk:Activity>
                        <trk:ActivityLocation>
                            <trk:Address>
                                <trk:City>Leeds</trk:City>
                                <trk:CountryCode>GB</trk:CountryCode>
                            </trk:Address>
                            <trk:Description>Reception</trk:Description>
                        </trk:ActivityLocation>
                        <trk:Status>
                            <trk:Type>I</trk:Type>
                            <trk:Description>Origin Scan</trk:Description>
                            <trk:Code>OR</trk:Code>
                        </trk:Status>
                        <trk:Date>20230118</trk:Date>
                        <trk:Time>163413</trk:Time>
                    </trk:Activity>
                    <trk:Activity>
                        <trk:ActivityLocation>
                            <trk:Address>
                                <trk:CountryCode>GB</trk:CountryCode>
                            </trk:Address>
                            <trk:Description>Reception</trk:Description>
                        </trk:ActivityLocation>
                        <trk:Status>
                            <trk:Type>M</trk:Type>
                            <trk:Description>Shipper created a label, UPS has not received the package yet.</trk:Description>
                            <trk:Code>MP</trk:Code>
                        </trk:Status>
                        <trk:Date>20230118</trk:Date>
                        <trk:Time>162032</trk:Time>
                    </trk:Activity>
                    <trk:PackageWeight>
                        <trk:UnitOfMeasurement>
                            <trk:Code>KGS</trk:Code>
                        </trk:UnitOfMeasurement>
                        <trk:Weight>1.00</trk:Weight>
                    </trk:PackageWeight>
                    <trk:ReferenceNumber>
                        <trk:Code>19</trk:Code>
                        <trk:Value>757142/1</trk:Value>
                    </trk:ReferenceNumber>
                    <trk:ReferenceNumber>
                        <trk:Code>19</trk:Code>
                        <trk:Value>75086</trk:Value>
                    </trk:ReferenceNumber>
                </trk:Package>
            </trk:Shipment>
        </trk:TrackResponse>
    </soapenv:Body>
</soapenv:Envelope>

The following code does seem to extract partial data but I don't see how to get the whole records out. Ideally I'd like to extract the records in a html table.

    $sxe = new SimpleXMLElement($client->__getLastResponse());

    $sxe->registerXPathNamespace('trk', "http://www.ups.com/XMLSchema/XOLTWS/Track/v2.0");

    $codes = $sxe->xpath('//trk:AddressLine');
    foreach ($codes as $c) {
        echo "$c" . "</br>";
      } 
Pete North
  • 150
  • 1
  • 1
  • 8
  • 1
    I tried multiple answers from [this question](https://stackoverflow.com/questions/6578832/how-to-convert-xml-into-array-in-php) and on first glance it seems [that one](https://stackoverflow.com/a/46349713/5527985) is achieving the desired result? Try if [this demo at 3v4l.org](https://3v4l.org/TH9k6#v8.2.1) helps ([-> only `["trk:Shipment"]`](https://3v4l.org/A0jvI#v8.2.1)). – bobble bubble Jan 24 '23 at 11:00
  • Thanks. That's converting it to json? I'm trying to get the data out in table format. Is there a function to decode that string to get data out? – Pete North Jan 24 '23 at 11:15
  • It's converted to an array, you need to generate the table yourself. – bobble bubble Jan 24 '23 at 11:24
  • Is there a way to query it? I don't understand what it's converted to. – Pete North Jan 24 '23 at 11:33
  • If you don't know about arrays [learn about them](https://www.php.net/manual/de/language.types.array.php). :) Search the web for terms like `generate table from array in php` or maybe ask new question. – bobble bubble Jan 24 '23 at 12:29
  • 1
    [Here is a demo](https://3v4l.org/AWoDR#v8.2.1) for getting started. I guess it will be quite a challenge to generate a nice table from this data. – bobble bubble Jan 25 '23 at 11:22
  • Thank you that! Put me in the right direction. – Pete North Jan 31 '23 at 15:10

0 Answers0