Sunday 26 January 2014

USPS Shipping

Calculate USPS Shipping Cost in PHP

USPS - How to Calculate Shipping Cost in PHP

Shipping Cost Calculation


Below script calculates domestic shipping cost from zip origination to zip destination. Put your userid in the xml accordingly. Find below the explanation.
 $devurl = "http://testing.shippingapis.com/ShippingAPITest.dll";
 $liveurl = "http://production.shippingapis.com/ShippingAPI.dll";
 
 $service = "RateV4";
 
 $xml = rawurlencode('<RateV4Request USERID="xxxx">
   <Revision>2</Revision>
   <Package ID="1ST">
   <Service>PRIORITY CPP</Service>
   <ZipOrigination>44106</ZipOrigination>
   <ZipDestination>20770</ZipDestination>
   <Pounds>0</Pounds>
   <Ounces>3.5</Ounces>
   <Container/>
   <Size>REGULAR</Size>
   <Machinable>true</Machinable>
   </Package>
   </RateV4Request>'); 
 
   $request = $devurl . "?API=" . $service . "&xml=" . $xml;
   $session = curl_init();
 
   curl_setopt($session, CURLOPT_URL, $request);
     curl_setopt($session, CURLOPT_HTTPGET, 1); 
     curl_setopt($session, CURLOPT_HEADER, false);
     curl_setopt( $session , CURLOPT_SSL_VERIFYPEER , false );
     curl_setopt( $session , CURLOPT_SSL_VERIFYHOST , false );
     curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
     curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
   
     if(ereg("^(https)",$devurl)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
   $response = curl_exec($session);
   curl_close($session); 
   $xml = new SimpleXMLElement($response);
   $rate = $xml->Package->Postage->Rate; 
   echo '
Rate '.$rate;
Below script calculates International shipping cost. Here we will use "IntlRateV2" service. Here you have to specify value of shipping items and destination country. Response includes a number of services varying in cost and delivery time. Here I have converted xml to array using "xml2array" function. You can find it on internet or contact me. After converting into array, we can parse the services. Below I am calculating the chepest service to India.
 $devurl = "http://testing.shippingapis.com/ShippingAPITest.dll";
 $liveurl = "http://production.shippingapis.com/ShippingAPI.dll";
 
 $service = 'IntlRateV2';
 
$country = 'India';
$xml = rawurlencode('<IntlRateV2Request USERID="xxx">
	<Package ID="1ST">
    <Pounds>15</Pounds>
    <Ounces>0</Ounces>
    <MailType>Package</MailType>
    <ValueOfContents>50</ValueOfContents>
    <Country>'.$country.'</Country>
    <Container>RECTANGULAR</Container>
    <Size>LARGE</Size>
    <Width>10</Width>
    <Length>15</Length>
    <Height>10</Height>
    <Girth>0</Girth>
	<CommercialFlag>N</CommercialFlag>
    </Package>  
</IntlRateV2Request>');
 
   $request = $devurl . "?API=" . $service . "&xml=" . $xml;
   $session = curl_init();
 
   curl_setopt($session, CURLOPT_URL, $request);
     curl_setopt($session, CURLOPT_HTTPGET, 1); 
     curl_setopt($session, CURLOPT_HEADER, false);
     curl_setopt( $session , CURLOPT_SSL_VERIFYPEER , false );
     curl_setopt( $session , CURLOPT_SSL_VERIFYHOST , false );
     curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
     curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
   
     if(ereg("^(https)",$devurl)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
   $response = curl_exec($session);
   curl_close($session); 
   $xml = new SimpleXMLElement($response);
   $arr = xml2array($request); //echo '
'; print_r($arr);
    $Services = $arr['IntlRateV2Response']['Package']['Service']; //echo '
'; print_r($Services);
    $rate = 0; $n=1;
    foreach($Services as $service)
    {
      if(isset($service['Postage']))	
      {
          if($n==1){$rate=$service['Postage']; $n++; continue;}
          
          if($rate>$service['Postage'])	
          $rate=$service['Postage'];
      }
    }
    echo '
Lowest International rate to '.$country.' - '.$rate;

How to use USPS API -

  • First regiter at USPS website (http://www.usps.com/webtools). you will receive an email, containing a user ID that will allow you to begin sending calls to the "test server"
  • USPS User ID has some restriction. Provided user id can not be used on multiple website.If the U.S. Postal Service discovers use of the same user ID from more than one web site, all users will be subject to immediate loss of access to the USPS server and termination of the licenses granted under the Terms and Conditions of Use
  • The next step is to test your API interfaces. As a registered API user you have been granted access to the Shipping API test server. An important note: the test server is set up to only accept a subset of pre-defined XML transactions and return the pre-defined XML responses provided in this document.
  • Once you have run your XML test transactions and have confirmed the XML responses, contact the Internet Customer Care Center (ICCC) (e-mail: uspstechsupport@esecurecare.net; telephone: 1-800-344-7779 (7:00AM to 11:00PM EST)) and request activation to the Shipping API production server. The ICCC will validate you for production access and will provide the Shipping API production server URL.
  • Once you have been validated by the ICCC, you will be able to connect to the Shipping API production server. Unlike the test server, the production server will accept live data. Note that once you have access to the production server you may still test against the test server.