i can't get this thing to work... i'm not getting any error messages back at.
Code: Select all
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
SauShip Developer Code Piece
Version 0.2.1 2003-03-03
(C) Copyright 2002-2003 Sauen.Com and Stoker (Jon T Stokkeland)
Sauen.Com, 302 East State Street, Salamanca, New York, 14779 sauship@sauen.com
This Code is Open/Free to use with the PHP, BSD, MPL or GPL License at your liking.
!WARNING!
This is just a bunch of code that happens to be useful if you are doing
UPS/USPS API integrations, it is not a solution of any kind and can only be
implemented and tested with the UPS/USPS systems by developers who have
developers access login/keys and have read and agreed to their terms.
Be aware of that UPS and USPS have very strict licensing and software packaging
rules, so you can not include this code in any product for sale or open source
application, you can only implement it on your own as the end user, or a developer
or consultant working on custom integrations directly for the end user which has
read and understood the terms from UPS/USPS.
This function-collection also includes code written by and Copyright by Hans Anderson 2003
Two XML functions, useful stuff: http://www.hansanderson.com/php/xml/xmlize.inc.txt
BTW: When doing USPS stuff, make sure you read the documentation very carefully,
their test server is very limited (idiotic) and will only respond correctly on the predefined
canned tests, change any value and you get an error, stupidity if you ask me,
or simply incompetent personel that created the system for USPS.. Also, their
servers does NOT accept anything else than GET, they never heard of POST I guess..
The production server is quite a bit more forgiving, allows more correct XML etc.
.. Will create some docs some day ..
# Requires PHP with curl (and ssl for UPS) The USPS part can easily be rewritten
# to not use curl.. The UPS part could use command line curl if you need to.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# All functions and global variables are prefixed with sauship_ (as a namespace).
# I did not create this as a class, mainly because PHP is not very efficient at OO code.
# If you would like a single-class version of this I can certainly create that for you
# Use this to debug
define('DEBUG',1);
### Public Functions
#############################
# USPS
function sauship_usps_domestic_quote (
$usps_server_url, # The server post URL provided to you by USPS
$username, # Yoyr USPS user
$password, # Your USPS password/access code
$weight, # Actual Weight in pounds (LB)
$from_zip,
$to_zip,
$container='None', # 0-109(n) | EP13(x) | EP14 | EP14F | None
$psize='Regular', # Regular | Large | Oversize
$machinable='True', # Used for Parcel only
$service=array('Priority','Express','Parcel') # Array with many for multi-quote or single-string
)
{
# Very little error control for now
$timeout = 30;
if (!is_array($service)) $svc = array($service);
else $svc = &$service;
$p = '';
$i = 0;
$pounds = (int) floor($weight);
$ounces = (int) ceil(((float)$weight - (float) $pounds) * 16); // Round up to nearest ounce
foreach ($svc as $s) {
if ($s === 'Parcel') $m = sauship_xmltag('Machinable',0,$machinable,1)."\n";
else $m = '';
$p .= "\n".
sauship_xmltag('Package',array('ID'=> $i++),
"\n".sauship_xmltag('Service',0,$s,1)."\n"
.sauship_xmltag('ZipOrigination',0,$from_zip,1)."\n"
.sauship_xmltag('ZipDestination',0,$to_zip,1)."\n"
.sauship_xmltag('Pounds',0,$pounds,0)."\n"
.sauship_xmltag('Ounces',0,$ounces,0)."\n"
.sauship_xmltag('Container',0,$container,1)."\n"
.sauship_xmltag('Size',0,$psize,1)."\n"
.$m
,0);
}
$x =
sauship_xmltag('RateRequest',array(
'USERID' => htmlspecialchars($username),
'PASSWORD' => htmlspecialchars($password)
),
$p,
0
);
$request = $usps_server_url.'?API=Rate&XML='.urlencode($x);
if (DEBUG) echo "<pre>DEBUG:\n\nRequest:\n".htmlspecialchars($x) ."\n\n".
htmlspecialchars($request)."\n\n";
# Do some fopen() or file() here instead if you don't have curl
$curlsession = curl_init();
@curl_setopt ($curlsession, CURLOPT_URL, $request);
curl_setopt ($curlsession, CURLOPT_TIMEOUT, $timeout);
curl_setopt ($curlsession, CURLOPT_HEADER, 0);
curl_setopt ($curlsession, CURLOPT_RETURNTRANSFER, 1);
$response = @curl_exec ($curlsession);
$curlinfo = curl_getinfo($curlsession);
if (!$response) {
$curl_error = curl_error($curlsession);
$curl_errno = curl_errno($curlsession);
$error = 'Curl error: '.$curl_errno.' '.$curl_error."\nConnection details:\n";
if (is_array($curlinfo)) {
while ($row = each($curlinfo)) {
$curl_info .= ' ' . $rowї0] . ' => ' . $rowї1] . "\n";
}
} else {
$curl_info .= ' '.$curlinfo. ' ::: The request: '.serialize($request);
}
$GLOBALSї'sauship_error'] = $curlinfo; // Use this to debug
return 0;
}
curl_close($curlsession);
if (DEBUG) echo "\n\nResponse:\n".htmlspecialchars($response)."\n\n-----\n\n";
unset ($request);
$xmld = sauship_xmlize($response);
if (!$xmld) {
$GLOBALSї'sauship_error'] = 'Xmlize failed, nothing returned. Response: '.serialize($response); // Use this to debug
return 0;
}
unset ($response);
if (DEBUG) var_dump($xmld);
// If there where no rates found you can use this var to debug it - comment out if you don't want it..
$GLOBALSї'sauship_response'] = $response;
$ratex = $xmldї'RateResponse']ї'#']ї'Package'];
if (!is_array($ratex)) {
$GLOBALSї'sauship_error'] = 'Rateportion missing: '.serialize($response)."\n\nXmlized: ".serialize($xmld); // Use this to debug
// This could occur if your weight was way high and no rating was available, therefor I set this global var u can auto-debug with
$GLOBALSї'sauship_norate'] = 1;
return 0;
}
unset($xmld);
foreach ($ratex as $cherr) if ($cherrї'#']ї'Error']) {
$err1 = (int) $cherrї'#']ї'Error']ї0]ї'#']ї'Number']ї0]ї'#'];
if ($err1 === -2147219498 ||
$err1 === -2147219453 ) $GLOBALSї'sauship_wrongzip'] = 1;
else $GLOBALSї'sauship_norate'] = 1;
// This could happen if there is an error in just one part, the rest may be ok..
$GLOBALSї'sauship_error'] = 'Errorcode in response: '.serialize($ratex);
return 0;
}
$rates = array();
foreach ($ratex as $rate) {
$ratesї(string) $rateї'#']ї'Service']ї0]ї'#']] = $rateї'#']ї'Postage']ї0]ї'#'];
}
if (DEBUG) { echo "\nParse result:\n"; print_r($rates); echo "\n--\n"; }
return $rates;
}
###############################################################################################
###
### Internal functions
###
// This function was written by and is Copyright by Hans Anderson 2003 - http://www.hansanderson.com/php/xml/xmlize.inc.txt - PHP License
function sauship_xmlize($data) {
$data = trim($data);
$vals = $index = $array = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $vals, $index);
xml_parser_free($parser);
$i = 0;
$tagname = $valsї$i]ї'tag'];
if ( isset ($valsї$i]ї'attributes'] ) ) {
$arrayї$tagname]ї'@'] = $valsї$i]ї'attributes'];
} else {
$arrayї$tagname]ї'@'] = array();
}
$arrayї$tagname]ї"#"] = sauship_xml_depth($vals, $i);
return $array;
}
// This function was written by and is Copyright by Hans Anderson 2003 - http://www.hansanderson.com/php/xml/xmlize.inc.txt - PHP License
function sauship_xml_depth($vals, &$i) {
$children = array();
if ( isset($valsї$i]ї'value']) ) {
array_push($children, $valsї$i]ї'value']);
}
while (++$i < count($vals)) {
switch ($valsї$i]ї'type']) {
case 'open':
if ( isset ( $valsї$i]ї'tag'] ) ) {
$tagname = $valsї$i]ї'tag'];
} else {
$tagname = '';
}
if ( isset ( $childrenї$tagname] ) ) {
$size = sizeof($childrenї$tagname]);
} else {
$size = 0;
}
if ( isset ( $valsї$i]ї'attributes'] ) ) {
$childrenї$tagname]ї$size]ї'@'] = $valsї$i]ї"attributes"];
}
$childrenї$tagname]ї$size]ї'#'] = sauship_xml_depth($vals, $i);
break;
case 'cdata':
array_push($children, $valsї$i]ї'value']);
break;
case 'complete':
$tagname = $valsї$i]ї'tag'];
if( isset ($childrenї$tagname]) ) {
$size = sizeof($childrenї$tagname]);
} else {
$size = 0;
}
if( isset ( $valsї$i]ї'value'] ) ) {
$childrenї$tagname]ї$size]ї"#"] = $valsї$i]ї'value'];
} else {
$childrenї$tagname]ї$size]ї"#"] = '';
}
if ( isset ($valsї$i]ї'attributes']) ) {
$childrenї$tagname]ї$size]ї'@'] = $valsї$i]ї'attributes'];
}
break;
case 'close':
return $children;
break;
}
}
return $children;
}
# Create the AccessRequest 'document' (UPS)
function sauship_accessrequest ( $username, $password, $xml_access_code ) {
return '<?xml version="1.0"?>'."\n"
. @sauship_xmltag('AccessRequest',array('xml:lang'=>'en-US'),
@sauship_xmltag('AccessLicenseNumber',0,$xml_access_code,1)
. @sauship_xmltag('UserId',0,$username,1)
. @sauship_xmltag('Password',0,$password,1)
);
}
# Create XML tag
function sauship_xmltag (
$tagname, # string
$params = array(), # Associative array
$data ='', # string
$convert_entities = 0 # konvert parameter values and data to legal entities
)
{
$result = "<$tagname";
if ($params) {
while (list ($key, $val) = each ($params)) {
if ($convert_entities) {
$val = str_replace('&','&',$val);
$val = str_replace('"','"',$val);
$val = str_replace('''',''',$val);
$val = str_replace('>','>',$val);
$val = str_replace('<','<',$val);
}
$result .= ' '.$key.'="'.addslashes($val).'"';
}
}
if (trim($data) !== '') {
if ($convert_entities) {
$data = str_replace('&','&',$data);
$data = str_replace('"','"',$data);
$data = str_replace('''',''',$data);
$data = str_replace('>','>',$data);
$data = str_replace('<','<',$data);
}
$result .= '>'.$data.'</'.$tagname.'>';
}
else $result .= " />";
return $result;
}
function sauship_ups_service_code ($code = '') {
static $c = array (
'01' => 'Next Day Air',
'02' => '2nd Day Air',
'03' => 'Ground',
'07' => 'Worldwide Express',
'08' => 'Worldwide Expedited',
'11' => 'Standard',
'12' => '3-Day Select',
'13' => 'Next Day Air Saver',
'14' => 'Next Day Air Early AM',
'54' => 'Worldwide Express Plus',
'59' => '2nd Day Air AM',
'65' => 'Express Saver'
);
if (empty($code)) return $c;
$code = sprintf('%02d',((int) $code));
if (empty($cї$code])) return;
return $cї$code];
}
function sauship_usps_service_code ($code = '') {
static $u = array (
'Express' => 'Express mail',
'First Class ' => 'First Class mail',
'Priority' => 'Priority mail',
'Parcel' => 'Parcel Post',
'BPM' => 'BPM - Bound Printed Matter',
'Library' => 'Library',
'Media' => 'Media',
);
if (empty($code)) return $u;
if (empty($uї$code])) return;
return $uї$code];
}
sauship_usps_domestic_quote (
'https://wwwcie.ups.com/ups.app/xml/Rate', # The server post URL
'myusername', # Yoyr USPS user
'mypassword', # Your USPS password/access code
'2', # Actual Weight in pounds (LB)
'28804',
'27534',
'None', # 0-109(n) | EP13(x) | EP14 | EP14F | None
'Regular', # Regular | Large | Oversize
'True', # Used for Parcel only
'Media' # Array with many for multi-quote or single-string
)
?>