Zend - The PHP Company




Commerce

Add Code


UPS Shipping Rates (XML)  

Type: application
Added by: quietbit
Entered: 29/09/2003
Last modified: 31/10/2002
Rating: - (fewer than 3 votes)
Views: 15429
Slighty messy but functional XML interface into the UPS system to retrieve shipping rates. Requires free registration on UPS website. Not intended for live system.


<?

/*
 functional UPS Rate interface quickly hacked out
 for proof of concept only, rewrite not publicly  available
*/


// NEED REGISTRATION OBTAINED VIA UPS SITE
$userid "upsuser";
$userid_pass "upspass";
$access_key "XXXXXXXXXXXXXXXX";


$activity "activity";

// DEFAULTS IF NOT SET
if (!$length)
  
$length 10;
if (!
$width)
  
$width 10;
if (!
$height)
  
$height 10;
if (!
$to_zip)
  
$to_zip '99999';
if (!
$from_zip)
  
$from_zip '99999';
if (!
$weight)
  
$weight 10;


// SERVICE TYPES
$service_codes = array();
$service_codes{'03'} = 'UPS Ground';
$service_codes{'02'} = 'UPS 2nd Day Air';
$service_codes{'01'} = 'UPS Next Day Air';
/*$service_codes{'07'} = 'UPS Worldwide Express';
$service_codes{'08'} = 'UPS Worldwide Expedited';
$service_codes{'11'} = 'UPS Standard';
$service_codes{'12'} = 'UPS 3 Day Select';
$service_codes{'13'} = 'UPS Next Day Air Saver';
$service_codes{'14'} = 'UPS Next Day Air Early A.M.';
$service_codes{'54'} = 'UPS Worldwide Express Plus';
$service_codes{'59'} = 'UPS 2nd Day Air A.M.';
$service_codes{'65'} = 'UPS Express Saver';*/

reset ($service_codes);

while (list(
$var,$val) = each ($service_codes)) {
  
$service_code $var;

$y "<?xml version="1.0"?><AccessRequest xml:lang="en-US"><AccessLicenseNumber>$access_key</AccessLicenseNumber><UserId>$userid</UserId><Password>$userid_pass</Password></AccessRequest><?xml version="1.0"?><RatingServiceSelectionRequest xml:lang="en-US"><Request><TransactionReference><CustomerContext>Bare Bones Rate Request</CustomerContext><XpciVersion>1.0</XpciVersion></TransactionReference><RequestAction>Rate</RequestAction><RequestOption>Rate</RequestOption></Request><PickupType><Code>01</Code></PickupType><Shipment><Shipper><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></Shipper><ShipTo><Address><PostalCode>$to_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipTo><ShipFrom><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipFrom><Service><Code>$service_code</Code></Service><Package><PackagingType><Code>02</Code></PackagingType><Dimensions><UnitOfMeasurement><Code>IN</Code></UnitOfMeasurement><Length>$length</Length><Width>$width</Width><Height>$height</Height></Dimensions><PackageWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>$weight</Weight></PackageWeight></Package></Shipment></RatingServiceSelectionRequest>"

// cURL ENGINE
$ch curl_init(); /// initialize a cURL session 
curl_setopt ($chCURLOPT_URL,"https://www.ups.com/ups.app/xml/Rate");
curl_setopt ($chCURLOPT_HEADER0);
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS"$y");
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
$xyz curl_exec ($ch);
curl_close ($ch);


$obj = new xml($xyz,"xml"); 

if (!
$i)
  
$i '0';

  if (
$extra == 1)
    print 
"$val,".$xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[$i
    .
",".$xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_BillingWeight"][0]->Weight[$i
    . 
"n"
  else
    print 
"$val,".$xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[$i] . "n"

$i++;

}


/*
 XML class code by Hans Anderson used for proof of 
 concept, complete proprietary rewrite for live system
 Thank You Mr. Anderson :)

(c) 2000 Hans Anderson Corporation. All Rights Reserved. 
You are free to use and modify this class under the same 
guidelines found in the PHP License. 

----------- 

bugs/me: 
http://www.hansanderson.com/php/ 
me@hansanderson.com 

*/


class xml_container 
    function 
store($k,$v) { 
        
$this->{$k}[] = $v
    } 


class 
xml 
    var 
$current_tag=array(); 
    var 
$xml_parser
    var 
$Version 1.0
    var 
$tagtracker = array(); 

    function 
startElement($parser$name$attrs) { 
        
array_push($this->current_tag$name); 
        
$curtag implode("_",$this->current_tag); 

        if(isset(
$this->tagtracker["$curtag"])) { 
            
$this->tagtracker["$curtag"]++; 
        } else { 
            
$this->tagtracker["$curtag"]=0
        } 

        if(
count($attrs)>0) { 
            
$j $this->tagtracker["$curtag"]; 
            if(!
$j$j 0

            if(!
is_object($GLOBALS[$this->identifier]["$curtag"][$j])) { 
                
$GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container
            } 

            
$GLOBALS[$this->identifier]["$curtag"][$j]->store("attributes",$attrs); 
        } 
    }

    function 
endElement($parser$name) { 
        
$curtag implode("_",$this->current_tag);     // piece together tag 

        
if(!$this->tagdata["$curtag"]) { 
            
$popped array_pop($this->current_tag); // or else we screw up where we are 
            
return;     // if we have no data for the tag 
        
} else { 
            
$TD $this->tagdata["$curtag"]; 
            unset(
$this->tagdata["$curtag"]); 
        } 

        
$popped array_pop($this->current_tag); 

        if(
sizeof($this->current_tag) == 0) return;     // if we aren't in a tag 

        
$curtag implode("_",$this->current_tag);     // piece together tag 
                                // this time for the arrays 

        
$j $this->tagtracker["$curtag"]; 
        if(!
$j$j 0

        if(!
is_object($GLOBALS[$this->identifier]["$curtag"][$j])) { 
            
$GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container
        } 

        
$GLOBALS[$this->identifier]["$curtag"][$j]->store($name,$TD); #$this->tagdata["$curtag"]); 
        
unset($TD); 
        return 
TRUE
    } 

    function 
characterData($parser$cdata) { 
        
$curtag implode("_",$this->current_tag); // piece together tag         
        
$this->tagdata["$curtag"] .= $cdata
    } 

    function 
xml($data,$identifier='xml') {   
        
$this->identifier $identifier

        
// create parser object 
        
$this->xml_parser xml_parser_create(); 

        
// set up some options and handlers 
        
xml_set_object($this->xml_parser,$this); 
        
xml_parser_set_option($this->xml_parser,XML_OPTION_CASE_FOLDING,0); 
        
xml_set_element_handler($this->xml_parser"startElement""endElement"); 
        
xml_set_character_data_handler($this->xml_parser"characterData"); 

        if (!
xml_parse($this->xml_parser$dataTRUE)) { 
            
sprintf("XML error: %s at line %d"
            
xml_error_string(xml_get_error_code($this->xml_parser)), 
            
xml_get_current_line_number($this->xml_parser)); 
        } 

        
// we are done with the parser, so let's free it 
        
xml_parser_free($this->xml_parser); 

    }  
// end constructor: function xml() 


}

?> 


Usage Example


See the example


Rate This Script





Search



This Category All Categories