With this class you can find out how much you have to pay for delivering product using UPS.
By : hermawan
<?php
/**
* UPS Shipping Calculation Class
* @author Hermawan Haryanto
* @date May 26, 2004
* @description
* This class is returning the cost of shipping using UPS
* Tested works for USA
* Please notify me if it's work for other contry
UPS PRODUCT CODE (this should be in a drop down menu)
Next Day Air Early AM 1DM
Next Day Air 1DA
Next Day Air Saver 1DP
2nd Day Air AM 2DM
2nd Day Air 2DA
3 Day Select 3DS
Ground GND
Canada Standard STD
Worldwide Express XPR
Worldwide Express Plus XDM
Worldwide Expedited XPD
UPS RATE CHART
Regular+Daily+Pickup
On+Call+Air
One+Time+Pickup
Letter+Center
Customer+Counter
Container Chart
Customers Packaging 00
UPS Letter Envelope 01
or
UPS Tube
UPS Express Box 21
UPS Worldwide 25kg Box 22
UPS Worldwide 10 kg Box 23
ResCom UPS Table
Residential 1
Commercial 2
*/
class ups
{
var $_action;
var $_delivery_code;
var $_src_zip;
var $_dst_zip;
var $_weight;
var $_src_country;
var $_dst_country;
var $_rate_chart;
var $_rate_charts;
var $_container;
var $_containers;
var $_rescom;
var $_rescoms;
var $_errors;
function ups()
{
$this->_errors = array();
$this->_action = 3;
$this->_delivery_code = 'GND';
$this->_src_country = 'US';
$this->_dst_country = 'US';
$this->_rate_chart = 1;
$this->_container = '0';
$this->_rescom = 1;
$this->_rate_charts = array('Regular+Daily+Pickup',
'On+Call+Air',
'One+Time+Pickup',
'Letter+Center',
'Customer+Counter');
$this->_containers = array ('00', '01', '21', '22', '23', '1', '2');
$this->_rescoms = array ('1', '2');
}
function set_rescom ($int)
{
$this->_rescom = $int;
}
function set_container ($int)
{
$this->_container = $int;
}
function set_rate_chart($int)
{
$this->_rate_chart = 1;
}
function set_action($int)
{
$this->_action = $int;
}
function set_delivery_code ($code)
{
$this->_delivery_code = $code;
}
function set_src_zip ($zip)
{
$this->_src_zip = $zip;
}
function set_dst_zip ($zip)
{
$this->_dst_zip = $zip;
}
function set_src_country ($code)
{
$this->_src_country = $code;
}
function set_dst_country ($code)
{
$this->_dst_country = $code;
}
function set_weight ($int)
{
$this->_weight = $int;
}
function get_cost ()
{
if ($this->_action == '')
array_push ($this->_errors, 'UPS Action is not defined');
if ($this->_delivery_code == '')
array_push ($this->_errors, 'UPS Product Code is not defined');
if ($this->_src_zip == '')
array_push ($this->_errors, 'Source Zip Code is unavailable');
if ($this->_src_country == '')
array_push ($this->_errors, 'Source Country Code is unavailable');
if ($this->_dst_zip == '')
array_push ($this->_errors, 'Destination Zip Code is unavailable');
if ($this->_dst_country == '')
array_push ($this->_errors, 'Destination Country Code is unavailable');
if ($this->_weight == '')
array_push ($this->_errors, 'Packet weight is not defined');
if ($this->_rate_chart == '')
array_push ($this->_errors, 'Rate Chart is not defined');
if ($this->_container == '')
array_push ($this->_errors, 'Client Shipping Package Type is not defined');
if ($this->_rescom == '')
array_push ($this->_errors, 'Residential or Commercial? define it first!');
if (count ($this->_errors) <= 0)
{
$url = 'www.ups.com';
$port = '80';
$file = '/using/services/rave/qcostcgi.cgi';
$qs = '?';
$qs .= 'accept_UPS_license_agreement=yes';
$qs .= '&';
$qs .= '10_action='.$this->_action;
$qs .= '&';
$qs .= '13_product='.$this->_delivery_code;
$qs .= '&';
$qs .= '14_origCountry='. $this->_src_country;
$qs .= '&';
$qs .= '15_origPostal='. $this->_src_zip;
$qs .= '&';
$qs .= '19_destPostal='. $this->_dst_zip;
$qs .= '&';
$qs .= '22_destCountry='. $this->_dst_country;
$qs .= '&';
$qs .= '23_weight='. $this->_weight;
$qs .= '&';
$qs .= '47_rateChart='.$this->_rate_charts[$this->_rate_chart];
$qs .= '&';
$qs .= '48_container='.$this->_rate_containers[$this->_rate_container];
$qs .= '&';
$qs .= '49_residential='.$this->_rescoms[$this->_rescom];
$rqs = $file.$qs;
$fp = @fsockopen ($url, 80, &$errno, &$errstr, 30);
if (!$fp) array_push ($this->_errors, 'Could not open socket');
else
{
fputs($fp,"GET $rqs HTTP/1.0\n\n");
while(!feof($fp))
{
$buffer = fgets ($fp, 1024);
if (strpos ($buffer, '%'))
{
$buffers = explode ('%', $buffer, -1);
$errcode = substr ($buffers[0], -1);
$goodcodes = array ('3', '4', '5', '6');
if (in_array ($errcode, $goodcodes))
{
$cost = trim ($buffers[8]);
return $cost;
}
else array_push ($this->_errors, 'Bad Result');
}
}
fclose($fp);
}
}
}
}
$ups = new ups;
$ups->set_src_zip('08053');
$ups->set_dst_zip('08055');
$ups->set_weight (5);
$ups_cost = $ups->get_cost();
if (!$ups_cost) print_r ($ups->_errors);
else print $ups_cost;
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. FREE! Go There Now!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download IBM DB2 Express-C 9.5, a no-charge version of DB2 Express 9 database server. DB2 Express-C offers the same core data server base features as other DB2 Express editions and provides a solid base to build and deploy applications developed using C/C++, Java, .NET, PHP, and other programming languages. FREE! Go There Now!
|
|
|
|
In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset. FREE! Go There Now!
|
|
|
|
Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs). FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
|
|
|
|
With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |