toolminator
asked on
Question: Help with an API for UPS
This is my first whack at a cgi interface to UPS's online tracking utilities, and none of my books
cover API's and online documentation is even less help.
If someone could offer some assistance, it would be MOST appreciated....
This is what I've got so far.....
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$ua->agent("AgentName/0.1 " . $ua->agent);
# Create a request
my $req = new HTTP::Request POST => 'https://www.ups.com/ups.app/xml/Rate';
$req->content_type('applic ation/x-ww w-form-url encoded');
$req->content('match=www&e rrors=0');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
print $res->content;
} else {
print "Error\n";
}
This was a tid bit that I managed to scrounge together starting out, but with the amount of variables
having to be sent in the request and then recieved in the response, is going to result in some pretty
sloppy code.
I'm hoping someone has done something similar and can give me an example of sending several variables in POST format during the request and then recieving several variables in the response.
Thankx all
Nat
cover API's and online documentation is even less help.
If someone could offer some assistance, it would be MOST appreciated....
This is what I've got so far.....
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$ua->agent("AgentName/0.1 " . $ua->agent);
# Create a request
my $req = new HTTP::Request POST => 'https://www.ups.com/ups.app/xml/Rate';
$req->content_type('applic
$req->content('match=www&e
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
print $res->content;
} else {
print "Error\n";
}
This was a tid bit that I managed to scrounge together starting out, but with the amount of variables
having to be sent in the request and then recieved in the response, is going to result in some pretty
sloppy code.
I'm hoping someone has done something similar and can give me an example of sending several variables in POST format during the request and then recieving several variables in the response.
Thankx all
Nat
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
toolminator,
Since you have found a solution to this question (https://www.experts-exchange.com/jsp/qShow.jsp?ta=cgi&qid=20194211), can you pl. go ahead and delete this question?
That will save you some points, & also keep the question list up-to-date.
Thanks,
Since you have found a solution to this question (https://www.experts-exchange.com/jsp/qShow.jsp?ta=cgi&qid=20194211), can you pl. go ahead and delete this question?
That will save you some points, & also keep the question list up-to-date.
Thanks,
ASKER
I like!
excellent example... worth every point...
manesh, sorry for overlooking the delete question. was actually still fishing for someone who had done the ups rate quotes. A thousand apologies....
excellent example... worth every point...
manesh, sorry for overlooking the delete question. was actually still fishing for someone who had done the ups rate quotes. A thousand apologies....
toolminator,
"..sorry for overlooking the delete question. was actually still fishing for someone who had done
the ups rate quotes. A thousand apologies.... .."
I didnt mean to sound mean & apologize if my comments came out that way.
My intention is to keep the PERL & CGI topic area up-to-date and free of duplicates.
Rgds,
Manesh
"..sorry for overlooking the delete question. was actually still fishing for someone who had done
the ups rate quotes. A thousand apologies.... .."
I didnt mean to sound mean & apologize if my comments came out that way.
My intention is to keep the PERL & CGI topic area up-to-date and free of duplicates.
Rgds,
Manesh
// **************************
// class UPSShipment extends Shipment:
// Class for UPS shipping prices and information.
// **************************
class UPSShipment extends Shipment
{
var $nda;
var $sda;
var $tds;
var $gro;
var $parent;
var $NEXT_DAY = "1DA";
var $SECOND_DAY = "2DA";
var $THIRD_DAY = "3DS";
var $GROUND = "GND";
function UPSShipment()
{
$this->parent = new Shipment();
$this->server = "http://www.ups.com:80/using/services/rave/qcost_dss.cgi?";
$this->keys = array( "UPSOnLine",
"AppVersion",
"ReturnCode",
"MessageText",
"ActionCode",
"ServiceLevelCode",
"ShipperPostalCode",
"ShipperCountry",
"ConsigneePostalCode",
"ConsigneeCountry",
"DeliveryZone",
"PackageActualWeight",
"ProductCharge",
"AccessorySurcharge",
"TotalCharge",
"CommitTime" );
$this->nda = 0;
$this->sda = 0;
$this->tds = 0;
$this->gro = 0;
$this->maxWeight = 150;
$this->msg = "<br>Unable to connect to the UPS server at " . $this->server . "<br>";
}
// *****
// Set the status
function setStatus( $get )
{
$retval = ( $this->getValue( $get ) == "0000" ) ? true : false;
return $retval;
}
function getNextDayAirPrice()
{
return sprintf( "%01.2f", $this->nda );
}
function getSecondDayAirPrice()
{
return sprintf( "%01.2f", $this->sda );
}
function getThreeDaySelectPrice()
{
return sprintf( "%01.2f", $this->tds );
}
function getGroundPrice()
{
return sprintf( "%01.2f", $this->gro );
}
// *****
// Set a message, if applicable
function setMessage( $get )
{
return $this->getValue( $get );
}
// *****
// Set the price
function setPrice( $get )
{
return $this->getValue( $get );
}
// *****
// Return the service type for the current line
function currentServiceType( $get )
{
return $this->getValue( $get );
}
function loadVals( $valLine, $delim = "%" )
{
$this->vals = array();
$tok = strtok( $valLine, $delim );
while( $tok )
{
$this->vals[count($this->v
$tok = strtok( $delim );
}
if ( empty( $this->retcode ) )
{
$this->retcode = $this->setStatus( "ReturnCode" );
$this->msg = $this->setMessage( "MessageText" );
}
if ( $this->success() )
{
if ( $this->currentServiceType(
$this->nda += $this->setPrice("TotalChar
if ( $this->currentServiceType(
$this->sda += $this->setPrice("TotalChar
if ( $this->currentServiceType(
$this->tds += $this->setPrice("TotalChar
if ( $this->currentServiceType(
$this->gro += $this->setPrice("TotalChar
}
}
function buildQueryString( $toCode, $fromCode, $weight, $resOrInd = "0", $toCountry = "US", $fromCountry = "US" )
{
// need to pass the application information
$this->queryString = "AppVersion=1.2&AcceptUPSL
// "ActionCode" 3 specifies a rate for a selected service; 4 specifies rates for all available
$this->queryString .= "ResponseType=application/
// "ServiceLevelCode" specifies which type of service available, even though we will get all (specified above)
$this->queryString .= "ServiceLevelCode=GND&Rate
// the "To" and "From" ZIP codes
$this->queryString .= "ShipperPostalCode=$fromCo
// "To" and "From" countries
$this->queryString .= "ConsigneeCountry=$fromCou
// the weight of this package
$this->queryString .= "PackageActualWeight=$weig
// "ResidentialInd": 0 is commercial; 1 is residential; "PackagingType" is provided by shipper
$this->queryString .= "ResidentialInd=$resOrInd&
}
function getResponse()
{
$fp = @fopen( $this->getServer() . $this->getQueryString(), 'r');
if( $fp == false )
return false;
while( $buffer = @fgets($fp, 4096) )
{
if ( strstr( $buffer, "UPSOnLine" ) )
$this->loadVals( $buffer );
}
@fclose( $fp );
return true;
}
} // end of class