Hi,
I am trying to make a php script to calculate the shiping charge
automatically by communicating to UPS online tool.
In order to do that, I used:
1. XML-RPC implemented by Keith Devens
http://www.keithdevens.com/software/xmlrpc/
2. PHP Version 4.3.3 w/ '--with-openssl'
3. The code below is for testing to see if I can communicate to UPS;
but no luck.
4. looks like the code successfully open the socket and send xml to
UPS.
5. but I dont' get anything back or retreiving the response is
invalid?
6. here is my code
7. Please somebody fix me.
<?php
/* Include the library */
include ( "kd_xmlrpc.php" );
/* Define variables to find the rpc server script */
$site = "www.ups.com:443";
$location = "/ups.app/xml/AV";
$upsAccessLicenseNumber = "myAccessLiscenseNumber"; //
2BAADC11591CC248
$upsUserID = "myID";
$upsPassWord = "myPW";
$upsXpciVersion = "1.0001";
function XMLRPC4UPSAV_request($city, $state=NULL, $zip=NULL){
global $site, $location, $upsAccessLicenseNumber, $upsUserID,
$upsPassWord, $upsXpciVersion;
list($site, $port) = explode(':', $site);
if(!is_numeric($port)){$port = 80;}
echo $site . "<br>" . $port . "<p>";
//Access Request
$accessData["AccessRequest"]["AccessLicenseNumber"] =
$upsAccessLicenseNumber;
$accessData["AccessRequest"]["UserId"] = $upsUserID;
$accessData["AccessRequest"]["Password"] = $upsPassWord;
$accessData = XML_serialize($accessData);
// Address Validation Request
$avData["AddressValidationRequest"]["Request"]["TransactionReference"]["CustomerContext"]
= "AV TEST";
$avData["AddressValidationRequest"]["Request"]["TransactionReference"]["XpciVersion"]
= $upsXpciVersion;
$avData["AddressValidationRequest"]["Request"]["RequestAction"] =
"AV";
$avData["AddressValidationRequest"]["Address"]["City"] = $city;
if(isset($state))$avData["AddressValidationRequest"]["Address"]["StateProvinceCode"]
= $state;
if(isset($zip))$avData["AddressValidationRequest"]["Address"]["PostalCode"]
= $zip;
$avData = XML_serialize($avData);
// combine Access Request & AV Request
$data = $accessData . $avData;
$conn = fsockopen ($site, $port); #open the connection
if(!$conn){ #if the connection was not opened successfully
echo "Failed";
}else{
$headers =
"POST $location HTTP/1.0\r\n" .
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n\r\n";
fputs($conn, $headers);
fputs($conn, $data);
$response = "";
while(!feof($conn)){ $response .= fgets($conn, 1024); }
fclose($conn);
#strip headers off of response
$data = XML_unserialize(substr($response, strpos($response,
"\r\n\r\n")+4));
return $data;
}
}
?>
<html>
<head>
<title> UPS Address Validator</title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="Jason">
<meta name="Keywords" content="XML RPC">
<meta name="Description" content="UPS AV">
</head>
<body>
<?
print_r(XMLRPC4UPSAV_request("Little Ferry", "NJ"));
?>
</body>
</html>