HELP WITH UPS CODE!!!
Posted: Fri Mar 02, 2007 9:25 am
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
HELLO EVERYONE!
I am trying to get this code to work. It is a script that connects to the ups site and gets the shipping rates.
I have had to block out my credentials.
The code works fine when I uncomment the last output of the while loop. But If I try to store the data into an array ($results) It creates the array rows but the values are empty. And helps is greatly appreciated.
Thanks!
The code:Code: Select all
<?php
//First we must get the cart weight.
// NEED REGISTRATION OBTAINED VIA UPS SITE
$userid = "XXXXXXXX";
$userid_pass = "XXXXX";
$access_key = "XXXXXXXXXXXXXXXXXXX";
$activity = "activity";
// DEFAULTS IF NOT SET
if (!$length)
$length = 10;
if (!$width)
$width = 10;
if (!$height)
$height = 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);
$results = array();
//Define the variables
$from_zip = "33765";
$to_zip = "91506";
$weight = 3;
//End define
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
if($ch)
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_URL, "https://www.ups.com/ups.app/xml/Rate");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $y);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$xyz = curl_exec ($ch);
curl_close ($ch);
$obj = new xml($xyz,"xml");
if (!$i)
$i = '0';
if ($extra == 1)
$results[$val] = $xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[$i];
/* print "<option value=\"$val\">$val - $".$xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[$i]
."</option>"; */
else
$results[$val] = $xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[$i];
/* print "<option value=\"$val\">$val - $".$xml["RatingServiceSelectionResponse_RatedShipment_RatedPackage_TotalCharges"][0]->MonetaryValue[$i]."</option>"; */
$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, $data, TRUE)) {
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()
}
?>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]