[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: USPS socket example



Here is an example that uses some of Scott's two geo-coder examples (from
the list, not the included examples) to use the USPS verify function. The
verify function in this case returns the USPS address coding. I am only
displaying the ADDRESS2, ZIP5 and ZIP4 information. There are more nuances
to the functions (see documentation for information).

The Incoming procedure breaks apart the information into its components and
handles some error conditions by displaying messages. If you run the program
as-is, you will see an error caused by the bad USPSUserID. If you put in a
valid USPSUserID, you will get back the corrected address information. If
you change the state (or any other field?), you will see a different error.

Mike Krebs
<code>
      *  This example acts as a web service consumer.  It performs an
      *  address standardization using the United States Postal Service
      *  (USPS) Web Tools. This example is using the test server only
      *  with the test data for address verification 1. The test server
      *  will only verify two addresses (see documentation for other).
      *
      *  Before using this, you need to get a developer's ID from
      *  the USPS.  Doing so only takes 3 minutes, just point your
      *  web browser to the following link.
      *
      *  http://www.usps.com/webtools/
      *
      *  Please note the terms and coniditions (and you get even more
      *  after you register). This API is suppose to be used on a web
      *  site to validate entered information, not to clean your
      *  database.
      *
      *  When you get your userID, put it in USPSUserID.
      *
     h dftactgrp(*no) bnddir('HTTPAPI')

      /copy httpapi_h

     D Incoming        PR
     D   res                               likeds(Result)
     D   depth                       10I 0 value
     D   name                      1024A   varying const
     D   path                     24576A   varying const
     D   value                    32767A   varying const
     D   attrs                         *   dim(32767)
     D                                     const options(*varsize)

     D Result          ds                  qualified
     D   Address1                    38a
     D   Address2                    38a
     D   City                        15a
     D   State                        2a
     D   zip5                         5a
     D   zip4                         4a

     D Enc             s                   like(HTTP_URL_ENCODER)
     D soap            s           2000a   varying
     D rc              s             10i 0

     D USPSUserID      s             12a   inz('999AAAAA9999')
     D address1        s             38a   inz('')
     D address2        s             38a   inz('6406 Ivy Lane')
     D city            s             15a   inz('Greenbelt')
     D state           s              2a   inz('MD')
     D zip5            s              5a   inz('')
     D zip4            s              4a   inz('')
     D uri             s           1024a   inz('') Varying
     D url             s          32767A   Varying
     D api             s           1024a   inz('') Varying
     D XML             s           1024a   inz('') Varying

      /free
        http_debug(*on);
        uri = 'http://testing.shippingapis.com/ShippingAPITest.dll?';
        api = 'API=Verify&';
        XML = '<AddressValidateRequest USERID="' + %trim(USPSUserID) +
              '"><Address ID="0">' +
              '<Address1>'+%trim(address1)+'</Address1>' +
              '<Address2>'+%trim(address2)+'</Address2>' +
              '<City>'+%trim(City)+'</City>' +
              '<State>'+%trim(State)+'</State>' +
              '<Zip5>'+%trim(zip5)+'</Zip5>' +
              '<Zip4>'+%trim(zip4)+'</Zip4>' +
              '</Address></AddressValidateRequest>' ;
        // Encode the XML to take care of spaces and other characters
        enc = http_url_encoder_new();
        http_url_encoder_addvar( enc
                                : 'XML'
                                : %addr(XML) + 2
                                : %len(%trimr(XML)) );
        url = uri + api + http_url_encoder_getstr(enc);

        http_setCCSIDs( 1208: 0 );

        rc = http_url_get_xml( url
                         : *NULL
                         : %paddr(Incoming)
                         : %addr(Result) );

        if (rc <> 1);
           http_crash();
        else;
           http_comp('Address2=' + %trim(Result.address2) + ', ' +
                     'Zip5=' + Result.zip5 + ', ' +
                     'Zip4=' + Result.zip4 );
        endif;

        *inlr = *on;

      /end-free

     P Incoming        B
     D Incoming        PI
     D   res                               likeds(Result)
     D   depth                       10I 0 value
     D   name                      1024A   varying const
     D   path                     24576A   varying const
     D   value                    32767A   varying const
     D   attrs                         *   dim(32767)
     D                                     const options(*varsize)

     d message         s             51a
     d messageResp     s              1a

      /free

         select;
         when name = 'Error';
            dsply 'Oops' ' ' messageResp;
         when name='Description';
            message = %subst(value:1:51);
            dsply message ' ' messageResp;
         when name = 'Address1';
            Res.Address1 = value;
         when name = 'Address2';
            Res.Address2 = value;
         when name = 'City';
            Res.City = value;
         when name = 'State';
            Res.State = value;
         when name = 'Zip5';
            Res.zip5 = value;
         when name = 'Zip4';
            Res.Zip4 = value;
          endsl;

      /end-free
     P                 E 

-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------