Search code examples
phpapiusps

How to handle exception cases in USPS API integration?


What message populates when an USPS Exception occurs? The API Name is Tracking & Delivery Information.

I have integrated USPS API successfully on my machine. I am able to get product status with tracking number, but there may be conditions when the exception can occur in API, I want to manage alert for that condition so that I can be informed for that case. I want to know how can I detect the exception?


Solution

  • I have found result myself

    But after lots of research I found the solution for my problem. which is given below:

    In USPS "Tracking & Delivery Information" API Integration there are Two Types of XML Request Format that we can send to USPS Server for retrieving the Parcel shipping response by its tracking number in two different way.

    1) Track/Confirm Web Tool:- This is intended for display purpose only and in response it returns text messages (summary and detail) only.

    API Signature(URL):
    http://production.shippingapis.com/ShippingAPI.dll?API=TrackV2   //Live server
    Or
    http://production.shippingapis.com/ShippingAPITest.dll?API=TrackV2  //Test server

    Request Parameters:

    <TrackRequest USERID=”xxxxxxxx”>
        <TrackID ID="EJ123456780US"></TrackID>
    </TrackRequest>
    

    It returns Response Parameters : summary and detail only

    <TrackResponse>
      <TrackInfo ID="E123456780US">
        <TrackSummary>
          Your item was delivered at 6:50 am on February 6 in BARTOW FL 33830.        
        </TrackSummary>
        <TrackDetail>February 6 6:49 am NOTICE LEFT BARTOW FL 33830</TrackDetail>
        <TrackDetail>February 6 6:48 am ARRIVAL AT UNIT BARTOW FL 33830</TrackDetail>
        <TrackDetail>February 6 3:49 am ARRIVAL AT UNIT LAKELAND FL 33805</TrackDetail>
        <TrackDetail>February 5 7:28 pm ENROUTE 33699</TrackDetail>
        <TrackDetail>February 5 7:18 pm ACCEPT OR PICKUP 33699</TrackDetail>
      </TrackInfo>
    </TrackResponse>
    

    2) Track/Confirm Fields Web Tool:- This is the request format that worked for me, I tested this by implementing this. This request returns full information of a parcel tracking number like:- API Signature(URL):Same as used for first one

    Request Example:

    <TrackFieldRequest USERID=" xxxxxxxx">
     <TrackID ID="01805213907042762274"></TrackID>
    </TrackFieldRequest>
    

    Response Example:

    <TrackResponse>
        <TrackInfo ID="01805213907042762274">
            <TrackSummary>
                <EventTime>12:12 pm</EventTime>
                <EventDate>May 21, 2001</EventDate>
                <Event>DELIVERED</Event>
                <EventCity>NEWTON</EventCity>
                <EventState>IA</EventState>
                <EventZIPCode>50208</EventZIPCode>
                <EventCountry/>
                <FirmName></FirmName>
                <Name></Name>
                <AuthorizedAgent></AuthorizedAgent>
            </TrackSummary>
            <TrackDetail>
                <EventTime>9:24 pm</EventTime>
                <EventDate>March 28, 2001</EventDate>
                <Event>ENROUTE</Event>
                <EventCity>DES MOINES</EventCity>
                <EventState>IA</EventState>
                <EventZIPCode>50395</EventZIPCode>
                <EventCountry/>
                <FirmName/>
                <Name/>
                <AuthorizedAgent/>
            </TrackDetail>
        .
        .
        .
    </TrackResponse>
    

    Note:I have Converted this XML response into simple PHP Array and used the "Event" filed for managing alert for all conditions Like ENROUTE/DELIVERED etc.. And manage error/exception that occurs by its error id which returns in its response array.

    See for More details