Creating UPS Rating ( XML,PHP using curl method)
index.php:-
<?php
// ini_set ('display_errors', 'true');
// error_reporting(E_ALL);
/*
Valid domestic values:
"14" = Next Day Air Early
AM
"01" = Next Day Air
"13" = Next Day Air Saver
"59" = 2nd Day Air AM
"02" = 2nd Day Air
"12" = 3 Day Select
"03" = Ground
Valid international values:
"11"= Standard
"07" = Worldwide Express
"54" = Worldwide Express Plus
"08" = Worldwide Expedited
"65" = Saver
Required for Rating and
Ignored for Shopping
Valid Poland to Poland
Same Day values:
"82" = UPS Today Standard
"83" = UPS Today Dedicated Courier
"84" = UPS Today Intercity
"85" = UPS Today Express
"86" = UPS Today Express Saver
Packaging Type Codes
"00" = "UNKNOWN"
"01" = UPS Letter
"02" = Package
"03" = Tube
"04" = Pak
"21" = Express Box
"24" = 25KG Box
"25" = 10KG Box
"30" = Pallet
"2a" = Small Express Box
"2b" = Medium Express Box
"2c" = Large Express Box
*/
if(isset($_REQUEST['fromzip'])){
$fromzip = $_REQUEST['fromzip'];
}else{
$fromzip = "21237";
}
if(isset($_REQUEST['tozip'])){
$tozip = $_REQUEST['tozip'];
}else{
$tozip = "21237";
}
if(isset($_REQUEST['width'])){
$width = $_REQUEST['width'];
}else{
$width = 6;
}
if(isset($_REQUEST['height'])){
$height = $_REQUEST['height'];
}else{
$height = 6;
}
if(isset($_REQUEST['length'])){
$length = $_REQUEST['length'];
}else{
$length = 6;
}
if(isset($_REQUEST['weight'])){
$weight = $_REQUEST['weight'];
}else{
$weight = 5;
}
?>
<form name="upsRate" action="index.php" method="post" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="a" value="step1" />
<div class="formline"> Shipping from Zipcode:
<input type="text" name="fromzip" value="<?php echo $fromzip; ?>" />
</div>
<div class="formline"> Shipping to Zipcode:
<input type="text" name="tozip" value="<?php echo $tozip; ?>" />
</div>
<div class="formline"> <strong>Package Dimensions (inches)</strong> </br>
Width:
<input type="text" class="short" name="width" value="<?php echo $width; ?>" />
</br>
Height:
<input type="text" class="short" name="height" value="<?php echo $height; ?>" />
</br>
Length:
<input type="text" class="short" name="length" value="<?php echo $length; ?>" />
</div>
</br>
<div class="formline"> Weight (LBS):
<input type="text" name="weight" class="short" value="<?php echo $weight; ?>" />
</div>
<div class="formline">
<input type="submit" />
</div>
</form>
<?php
error_reporting(E_ALL & ~E_NOTICE);
if(isset($_REQUEST['a']) && $_REQUEST['a'] == "step1"){
$errors = false;
$errmessage = "";
if(is_numeric($_REQUEST['fromzip'])){
$fromzip = $_REQUEST['fromzip'];
}else{
$errors = true;
$errmessage = "Please enter a valid Shipping from Zip.<br />\n";
}
if(is_numeric($_REQUEST['tozip'])){
$tozip = $_REQUEST['tozip'];
}else{
$errors = true;
$errmessage = "Please enter a valid Shipping from Zip.<br />\n";
}
if(is_numeric($_REQUEST['length'])){
$length = $_REQUEST['length'];
}else{
$errors = true;
$errmessage = "Please enter a valid length.<br />\n";
}
if(is_numeric($_REQUEST['width'])){
$width = $_REQUEST['width'];
}else{
$errors = true;
$errmessage = "Please enter a valid width.<br />\n";
}
if(is_numeric($_REQUEST['height'])){
$height=$_REQUEST['height'];
}else{
$errors = true;
$errmessage = "Please enter a valid height.<br />\n";
}
if(is_numeric($_REQUEST['weight'])){
$weight = $_REQUEST['weight'];
}else{
$errors = true;
$errmessage = "Please enter a valid weight.<br />\n";
}
if(!$errors){
require("upsRate.php");
/*************************************
Get your own credentials from ups.com
*************************************/
$ups_accessnumber = "Enter your Access Number Here";
$ups_username = " Enter your ups username Here ";
$ups_password = " Enter your ups password Here ";
$ups_shippernumber = " Enter your ups shippernumber Here ";
// just doing domestic for demonstration purposes
$services = array(
"Next Day Air (early AM)"=>"14",
"Next Day Air"=>"01",
"Next Day Air Saver"=>"13",
"2nd Day Air AM"=>"59",
"2nd Day Air"=>"02",
"3 Day Select"=>"12",
"Ground"=>"03"
);
$myRate = new upsRate;
$myRate->setCredentials($ups_accessnumber, $ups_username, $ups_password, $ups_shippernumber);
echo "<h1>UPS Rates</h1>\n";
echo "<ul>\n";
foreach($services as $name=>$value){
$service = $value;
$rate = $myRate->getRate($fromzip, $tozip, $service, $length, $width, $height, $weight);
echo "<li>$name - $rate</li>\n";
}
echo "</ul>";
}else{
echo "<h2>Form Errors:</h2><p>$errmessage</p>\n";
}
}
?>
upsRate.php:-
<?php
class upsRate {
var $AccessLicenseNumber;
var $UserId;
var $Password;
var $shipperNumber;
var $credentials;
var $dimensionsUnits = "IN";
var $weightUnits = "LBS";
function setCredentials($access,$user,$pass,$shipper) {
$this->AccessLicenseNumber = $access;
$this->UserID = $user;
$this->Password = $pass;
$this->shipperNumber = $shipper;
$this->credentials = 1;
}
function setDimensionsUnits($unit){
$this->dimensionsUnits = $unit;
}
function setWeightUnits($unit){
$this->weightUnits = $unit;
}
// Define the function getRate() - no parameters
function getRate($PostalCode,$dest_zip,$service,$length,$width,$height,$weight) {
if ($this->credentials != 1) {
print 'Please set your credentials with the setCredentials function';
die();
}
$data ="<?xml version=\"1.0\"?>
<AccessRequest xml:lang=\"en-US\">
<AccessLicenseNumber>$this->AccessLicenseNumber</AccessLicenseNumber>
<UserId>$this->UserID</UserId>
<Password>$this->Password</Password>
</AccessRequest>
<?xml version=\"1.0\"?>
<RatingServiceSelectionRequest xml:lang=\"en-US\">
<Request>
<TransactionReference>
<CustomerContext>Bare Bones Rate Request</CustomerContext>
<XpciVersion>1.0001</XpciVersion>
</TransactionReference>
<RequestAction>Rate</RequestAction>
<RequestOption>Rate</RequestOption>
</Request>
<PickupType>
<Code>01</Code>
</PickupType>
<Shipment>
<Shipper>
<Address>
<PostalCode>$PostalCode</PostalCode>
<CountryCode>US</CountryCode>
</Address>
<ShipperNumber>$this->shipperNumber</ShipperNumber>
</Shipper>
<ShipTo>
<Address>
<PostalCode>$dest_zip</PostalCode>
<CountryCode>US</CountryCode>
<ResidentialAddressIndicator/>
</Address>
</ShipTo>
<ShipFrom>
<Address>
<PostalCode>$PostalCode</PostalCode>
<CountryCode>US</CountryCode>
</Address>
</ShipFrom>
<Service>
<Code>$service</Code>
</Service>
<Package>
<PackagingType>
<Code>02</Code>
</PackagingType>
<Dimensions>
<UnitOfMeasurement>
<Code>$this->dimensionsUnits</Code>
</UnitOfMeasurement>
<Length>$length</Length>
<Width>$width</Width>
<Height>$height</Height>
</Dimensions>
<PackageWeight>
<UnitOfMeasurement>
<Code>$this->weightUnits</Code>
</UnitOfMeasurement>
<Weight>$weight</Weight>
</PackageWeight>
</Package>
</Shipment>
</RatingServiceSelectionRequest>";
$ch = curl_init("https://www.ups.com/ups.app/xml/Rate");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_TIMEOUT, 60);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
echo '<!-- '. $result. ' -->'; // THIS LINE IS FOR DEBUG PURPOSES ONLY-IT WILL SHOW IN HTML COMMENTS
$data = strstr($result, '<?');
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$params = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem['type'] == 'open') {
if (array_key_exists('attributes',$xml_elem)) {
list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
} else {
$level[$xml_elem['level']] = $xml_elem['tag'];
}
}
if ($xml_elem['type'] == 'complete') {
$start_level = 1;
$php_stmt = '$params';
while($start_level < $xml_elem['level']) {
$php_stmt .= '[$level['.$start_level.']]';
$start_level++;
}
$php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
eval($php_stmt);
}
}
curl_close($ch);
return $params['RATINGSERVICESELECTIONRESPONSE']['RATEDSHIPMENT']['TOTALCHARGES']['MONETARYVALUE'];
}
}
?>
Comments
Post a Comment