Wednesday, November 30, 2011

USPS Delivery Confirmation Web Tool Set Up Code Example

The USPS delivery confirmation web tool allows developers to integrate shipping labels into a site.  This is the code for setting up a sample label request using Php and XML. Note: If you read the USPS web tools documentation on this API they will tell you that you have to run test labels first before getting access to sample labels. This is a load of crap and you will waste your time trying to figure out why your code doesn't work. You must call the USPS and tell them that you want access to their sample labels and you will just use the https://secure.shippingapis.com/ShippingAPI.dll. Notice that you must use the secure https connection when sending this request because you are sending live data.  You will receive an encoded TIF or PDF image from the response.  You will need to decode the image and then use ImageMagick to show the shipping label image.  For more information and detailed examples go to www.uspswebtoolshelp.com



<?php
require_once("xmlparser.php");
$url = "https://secure.shippingapis.com/ShippingAPI.dll";
$ch = curl_init();    // initialize curl handle
curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // times out after 4s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, 'API=DelivConfirmCertifyV3&XML=<DelivConfirmCertifyV3.0Request USERID="USERNAME">
<Option>1</Option>
<ImageParameters></ImageParameters>
<FromName>bub Smith</FromName>
<FromFirm>ABC Corp.</FromFirm>
<FromAddress1>Apt. 3C</FromAddress1>
<FromAddress2>6406 Ivy Lane</FromAddress2>
<FromCity>Greenbelt</FromCity>
<FromState>MD</FromState>
<FromZip5>20770</FromZip5>
<FromZip4>1234</FromZip4>
<ToName>Tom Collins</ToName>
<ToFirm>XYZ Corp.</ToFirm>
<ToAddress1>Suite 4D</ToAddress1>
<ToAddress2>8 Wildwood Drive</ToAddress2>
<ToCity>Old Lyme</ToCity>
<ToState>CT</ToState>
<ToZip5>06371</ToZip5>
<ToZip4></ToZip4>
<WeightInOunces>32</WeightInOunces>
<ServiceType>Priority</ServiceType>
<SeparateReceiptPage>TRUE</SeparateReceiptPage>
<POZipCode></POZipCode>
<ImageType>TIF</ImageType>
<LabelDate></LabelDate>
<CustomerRefNo></CustomerRefNo>
<AddressServiceRequested></AddressServiceRequested>
<SenderName></SenderName>
<SenderEMail></SenderEMail>
<RecipientName></RecipientName>
<RecipientEMail></RecipientEMail>
</DelivConfirmCertifyV3.0Request>'); // add POST fields
$result = curl_exec($ch); // run the whole process
curl_close($ch);

 $xmlParser = new xmlparser();
        $array = $xmlParser->GetXMLTree($result);
print_r($array);

if(count($array['DELIVCONFIRMCERTIFYV3.0RESPONSE']))
{
$confirmation_number = $array['DELIVCONFIRMCERTIFYV3.0RESPONSE'][0]['DELIVERYCONFIRMATIONLABEL'][0]['VALUE'];
$tif = base64_decode($confirmation_number);
// Create Imagick object
        $im = new Imagick();

       // Convert image into Imagick
       $im->readimageblob($tif);
       $im->setCompressionQuality(90);
       $im->setImageFormat('jpeg');
       $im->resizeImage(500, 500, imagick::FILTER_LANCZOS, 1);
       $jpg = base64_encode($im);
       //resize
       echo '<img src="data:image/gif;base64,' . $jpg . '" />';
}
?>>

XML parser for USPS web tools Code example

When using USPS's web tools you will need to work with XML.  You can learn XML at w3schools.com Basically, you are sending an XML request via Php or Ajax and USPS will send you a response back in XML.  I am including an XML parser written in Php for you to use.  When you receive a response this parser will put the XML into an array and you can use if statements to gain access to the values of the XML response.


<?php
class xmlparser
{

    function GetChildren($vals, &$i) 
    {
        $children = array(); 
    
    
        if (isset($vals[$i]['value'])) 
            $children['VALUE'] = $vals[$i]['value']; 
    
    
        while (++$i < count($vals))
        { 
            switch ($vals[$i]['type']) 
            { 
                case 'cdata': 
                    if (isset($children['VALUE']))
                        $children['VALUE'] .= $vals[$i]['value']; 
                    else
                        $children['VALUE'] = $vals[$i]['value']; 
                    break;
    
                case 'complete': 
                    if (isset($vals[$i]['attributes'])) {
                        $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
                        $index = count($children[$vals[$i]['tag']])-1;
    
                        if (isset($vals[$i]['value'])) 
                            $children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value']; 
                        else
                            $children[$vals[$i]['tag']][$index]['VALUE'] = ''; 
                    } else {
                        if (isset($vals[$i]['value'])) 
                            $children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value']; 
                        else
                            $children[$vals[$i]['tag']][]['VALUE'] = ''; 
    }
                    break; 
    
                case 'open': 
                    if (isset($vals[$i]['attributes'])) {
                        $children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
                        $index = count($children[$vals[$i]['tag']])-1;
                        $children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],$this->GetChildren($vals, $i));
                    } else {
                        $children[$vals[$i]['tag']][] = $this->GetChildren($vals, $i);
                    }
                    break; 
    
                case 'close': 
                    return $children; 
            } 
        } 
    } 

    function GetXMLTree($xml) 
    { 
        $data = $xml;
       
        $parser = xml_parser_create();
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
        xml_parse_into_struct($parser, $data, $vals, $index); 
        xml_parser_free($parser);
        
        //print_r($index);
    
        $tree = array(); 
        $i = 0; 
        
        if (isset($vals[$i]['attributes'])) {
    $tree[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes']; 
    $index = count($tree[$vals[$i]['tag']])-1;
    $tree[$vals[$i]['tag']][$index] =    array_merge($tree[$vals[$i]['tag']][$index], $this->GetChildren($vals, $i));
        }
        else
            $tree[$vals[$i]['tag']][] = $this->GetChildren($vals, $i); 
        
        return $tree; 
    } 
    
    function printa($obj) {
        global $__level_deep;
        if (!isset($__level_deep)) $__level_deep = array();
    
        if (is_object($obj))
            print '[obj]';
        elseif (is_array($obj)) {
            foreach(array_keys($obj) as $keys) {
                array_push($__level_deep, "[".$keys."]");
                $this->printa($obj[$keys]);
                array_pop($__level_deep);
            }
        }
        else print implode(" ",$__level_deep)." = $obj\n";
    }
}
?>

USPS Shipping Rate Calculator API Set UP Code Examples

USPS provides a shipping rate calculator web tool which you can use if you sign up for one of their web tools accounts.  This is a very handy tool to have for any website that wants to display shipping information instantly to the browser.  Setting up this USPS web tool can be quite challenging and frustrating if you lack a lot programming experience.  This is a script that works for the rate calculator.  It is written in Php.  Assuming you have some object oriented programming experience,  you only need to implement your own user name and password into the script.  For more help with USPS web tools go to www.uspswebtoolshelp.com


<?php
require_once("xmlparser.php");


class USPS {


    var $server = "";
    var $user = "";
    var $pass = "";
    var $service = "";
    var $dest_zip;
    var $orig_zip;
    var $pounds;
    var $ounces;
    var $container = "None";
    var $size = "REGULAR";
    var $machinable;
    var $country = "USA";
    var $shipping_service = "";
    var $shipping_price;
   
    function setServer($server) {
        $this->server = $server;
    }
   
    function setShippingService($shipping_service){
    $this->shipping_service = $shipping_service;
    }


    function setUserName($user) {
        $this->user = $user;
    }


    function setPass($pass) {
        $this->pass = $pass;
    }


    function setService($service) {
        /* Must be: Express, Priority, or Parcel */
        $this->service = $service;
    }
   
    function setDestZip($sending_zip) {
        /* Must be 5 digit zip (No extension) */
        $this->dest_zip = $sending_zip;
    }


    function setOrigZip($orig_zip) {
        $this->orig_zip = $orig_zip;
    }


    function setWeight($pounds, $ounces=0) {
        /* Must weight less than 70 lbs. */
        $this->pounds = $pounds;
        $this->ounces = $ounces;
    }


    function setContainer($cont) {
        $this->container = $cont;
    }


    function setSize($size) {
        $this->size = $size;
    }


    function setMachinable($mach) {
        /* Required for Parcel Post only, set to True or False */
        $this->machinable = $mach;
    }
   
    function setCountry($country) {
        $this->country = $country;
    }
   
    function getMe()
    {
    return $me;
    }
   
    function getPrice() {
        if($this->country=="USA"){
            // may need to urlencode xml portion
            $str = $this->server. "?API=RateV2&XML=<RateV2Request%20USERID=\"";
            $str .= $this->user . "\"%20PASSWORD=\"" . $this->pass . "\"><Package%20ID=\"0\"><Service>";
            $str .= $this->service . "</Service><ZipOrigination>" . $this->orig_zip . "</ZipOrigination>";
            $str .= "<ZipDestination>" . $this->dest_zip . "</ZipDestination>";
            $str .= "<Pounds>" . $this->pounds . "</Pounds><Ounces>" . $this->ounces . "</Ounces>";
            $str .= "<Container>" . urlencode($this->container) . "</Container><Size>" . $this->size . "</Size>";
            $str .= "<Machinable>" . $this->machinable . "</Machinable></Package></RateV2Request>";
        }
        else {
            $str = $this->server. "?API=IntlRate&XML=<IntlRateRequest%20USERID=\"";
            $str .= $this->user . "\"%20PASSWORD=\"" . $this->pass . "\"><Package%20ID=\"0\">";
            $str .= "<Pounds>" . $this->pounds . "</Pounds><Ounces>" . $this->ounces . "</Ounces>";
            $str .= "<MailType>Package</MailType><Country>".urlencode($this->country)."</Country></Package></IntlRateRequest>";
        }


       
        $ch = curl_init();
        // set URL and other appropriate options
        curl_setopt($ch, CURLOPT_URL, $str);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 


        // grab URL and pass it to the browser
        $ats = curl_exec($ch);


        // close curl resource, and free up system resources
        curl_close($ch);
        $xmlParser = new xmlparser();
        $array = $xmlParser->GetXMLTree($ats);
        //$xmlParser->printa($array);
        if(count($array['ERROR'])) { // If it is error
            $error = new error();
            $error->number = $array['ERROR'][0]['NUMBER'][0]['VALUE'];
            $error->source = $array['ERROR'][0]['SOURCE'][0]['VALUE'];
            $error->description = $array['ERROR'][0]['DESCRIPTION'][0]['VALUE'];
            $error->helpcontext = $array['ERROR'][0]['HELPCONTEXT'][0]['VALUE'];
            $error->helpfile = $array['ERROR'][0]['HELPFILE'][0]['VALUE'];
            $this->error = $error;
        } else if(count($array['RATEV2RESPONSE'][0]['PACKAGE'][0]['ERROR'])) {
            $error = new error();
            $error->number = $array['RATEV2RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['NUMBER'][0]['VALUE'];
            $error->source = $array['RATEV2RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['SOURCE'][0]['VALUE'];
            $error->description = $array['RATEV2RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['DESCRIPTION'][0]['VALUE'];
            $error->helpcontext = $array['RATEV2RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['HELPCONTEXT'][0]['VALUE'];
            $error->helpfile = $array['RATEV2RESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['HELPFILE'][0]['VALUE'];
            $this->error = $error;      
        } else if(count($array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'])){ //if it is international shipping error
            $error = new error($array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR']);
            $error->number = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['NUMBER'][0]['VALUE'];
            $error->source = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['SOURCE'][0]['VALUE'];
            $error->description = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['DESCRIPTION'][0]['VALUE'];
            $error->helpcontext = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['HELPCONTEXT'][0]['VALUE'];
            $error->helpfile = $array['INTLRATERESPONSE'][0]['PACKAGE'][0]['ERROR'][0]['HELPFILE'][0]['VALUE'];
            $this->error = $error;
        } else if(count($array['RATEV2RESPONSE'])){ // if everything OK
            //print_r($array['RATEV2RESPONSE']);
            $this->zone = $array['RATEV2RESPONSE'][0]['PACKAGE'][0]['ZONE'][0]['VALUE'];
            foreach ($array['RATEV2RESPONSE'][0]['PACKAGE'][0]['POSTAGE'] as $value){
                $price = new price();
                $price->mailservice = $value['MAILSERVICE'][0]['VALUE'];
                $price->rate = $value['RATE'][0]['VALUE'];
                if($value['MAILSERVICE'][0]['VALUE'] == $this->shipping_service)
$this->shipping_price = $price->rate = $value['RATE'][0]['VALUE'];
                $this->list[] = $price;
            }
        } else if (count($array['INTLRATERESPONSE'][0]['PACKAGE'][0]['SERVICE'])) { // if it is international shipping and it is OK
            foreach($array['INTLRATERESPONSE'][0]['PACKAGE'][0]['SERVICE'] as $value) {
                $price = new intPrice();
                $price->id = $value['ATTRIBUTES']['ID'];
                $price->pounds = $value['POUNDS'][0]['VALUE'];
                $price->ounces = $value['OUNCES'][0]['VALUE'];
                $price->mailtype = $value['MAILTYPE'][0]['VALUE'];
                $price->country = $value['COUNTRY'][0]['VALUE'];
                $price->rate = $value['POSTAGE'][0]['VALUE'];
                $price->svccommitments = $value['SVCCOMMITMENTS'][0]['VALUE'];
                $price->svcdescription = $value['SVCDESCRIPTION'][0]['VALUE'];
                $price->maxdimensions = $value['MAXDIMENSIONS'][0]['VALUE'];
                $price->maxweight = $value['MAXWEIGHT'][0]['VALUE'];
                $this->list[] = $price;
            }
       
        }
        return $this;
    }
}
class error
{
    var $number;
    var $source;
    var $description;
    var $helpcontext;
    var $helpfile;
}
class price
{
    var $mailservice;
    var $rate;
}
class intPrice
{
    var $id;
    var $rate;
}
?>