ups api endpoints php test code address validation


We’re working with a WooCommerce client right now whose UPS shipping address validation and shipping cost calculations stopped working. The first issue we identified was the UPS shipping plugin they had was outdated and the core domain for the company that developed it had malware… that’s never a good sign. So, we purchased the WooCommerce UPS plugin since it’s well-supported by the developers of Woocommerce.

Ecommerce Marketing Automation

With the site not validating addresses nor integrating shipping, our first step was to verify that the UPS application programming interface (API) was up and operational. UPS does have a nice site for checking the status of its API.

Since the API didn’t appear to be done, our next step was to debug the issue locally. Interestingly enough, neither plugin had any logging nor testing to see if the UPS shipping integration actually worked. Even the debug setting didn’t provide any feedback, nor did our log files. So, in order to test the API, I had to program a script to actually test the API.

I downloaded the UPS API Developer Kit… which included code samples… and was confused as ever. The documentation is limited, the endpoints for the API weren’t even listed, and the code samples aren’t well-documented.

Download The UPS API Developer Kit

As a result, I had to do some digging… the first was to identify endpoints for their API. I found documented testing endpoints, wrote my code, and tested it… with no success. A little more digging and I found out that the testing endpoints were basically useless. Ugh.

UPS API Endpoints

I was able to find a thread on a development site that listed the UPS API production endpoints:

  • https://onlinetools.ups.com/ups.app/xml/TimeInTransit
  • https://onlinetools.ups.com/ups.app/xml/License
  • https://onlinetools.ups.com/ups.app/xml/QVEvents
  • https://onlinetools.ups.com/ups.app/xml/Register
  • https://onlinetools.ups.com/ups.app/xml/AV
  • https://onlinetools.ups.com/ups.app/xml/ShipAccept
  • https://onlinetools.ups.com/ups.app/xml/Void
  • https://onlinetools.ups.com/ups.app/xml/XAV
  • https://onlinetools.ups.com/ups.app/xml/Track
  • https://onlinetools.ups.com/ups.app/xml/Rate
  • https://onlinetools.ups.com/ups.app/xml/ShipConfirm
  • https://onlinetools.ups.com/ups.app/xml/LabelRecovery

The easiest one to test is the Address Validation (bold above) endpoint so I used the code provided to write a small PHP script that passed the address and responded with whether or not it was successful or failing. Here’s the code in the event you’d like to use it:

UPS API PHP Test File for Address Validation

Here’s the updated PHP script for testing the Address Validation UPS API Endpoint:

<html>
<head>UPS Address Validation Test</head>
<body>Response: <?php

// Configuration
$accessLicenseNumber = "Insert Your API Key";
$userId = "Insert Your User ID";
$password = "Insert Your Password";

$endpointurl="https://onlinetools.ups.com/ups.app/xml/AV";

try {
	
	// Create AccessRequest XMl
	$accessRequestXML = new SimpleXMLElement ( "<AccessRequest></AccessRequest>" );
	$accessRequestXML->addChild ( "AccessLicenseNumber", $accessLicenseNumber );
	$accessRequestXML->addChild ( "UserId", $userId );
	$accessRequestXML->addChild ( "Password", $password );
	
	// Create AddressValidationRequest XMl
	$avRequestXML = new SimpleXMLElement ( "<AddressValidationRequest ></AddressValidationRequest >" );
	$request = $avRequestXML->addChild ( 'Request' );
	$request->addChild ( "RequestAction", "AV" );
	
	$address = $avRequestXML->addChild ( 'Address' );
	$address->addChild ( "City", "ALPHARETTA" );
	$address->addChild ( "PostalCode", "300053778" );
	$requestXML = $accessRequestXML->asXML () . $avRequestXML->asXML ();
	
	$form = array (
			'http' => array (
					'method' => 'POST',
					'header' => 'Content-type: application/x-www-form-urlencoded',
					'content' => "$requestXML" 
			) 
	);
	
	// get request
	$request = stream_context_create ( $form );
	$browser = fopen ( $endpointurl, 'rb', false, $request );
	if (! $browser) {
		throw new Exception ( "Connection failed." );
	}
	
	// get response
	$response = stream_get_contents ( $browser );
	fclose ( $browser );
	
	if ($response == false) {
		throw new Exception ( "Bad data." );
	} else {
		
		// get response status
		$resp = new SimpleXMLElement ( $response );
		echo $resp->Response->ResponseStatusDescription . "n";
	}
	
} catch ( Exception $ex ) {
	echo $ex;
}

?>
</body>
</html>

This script will at least show you whether or not you’re credentials are working with the UPS API Address Validation endpoint. I realize the PHP methodology (fopen) to post to their API is a bit aged in this example above… but I just wanted to get their test code working.

Disclosure: I am using my WooCommerce affiliate links in this article.



Source link

30%* off! Big Savings For Your Small Business!

LEAVE A REPLY

Please enter your comment!
Please enter your name here