How to Implement UPS API and Create Shipping Label
To implement UPS api on your website credential required like access number, username and password. Get access number from UPS account.
Steps to Implement UPS API:
Step 1: Let me explain with an HTML Form example, which you can use your website for post shipping and billing information.
<form method="post" action="postshipping.php"> <input type="hidden" name="create_xml" value="true"> <h2>Bill To:</h2> <input type="text" name="sender_name" value="SENDER NAME" /> <br/> <input type="text" name="attention_name" value="ATTENTION NAME" /> <br/> <input type="text" name="Address_Line_1" value="ADDRESS LINE 1" /> <br/> <input type="text" name="city_name" value="CITY NAME" /> <br/> <input type="text" name="postal_code" value="POSTAL CODE" /> <br/> <input type="text" name="country_code" value="GB" /> <br/> <input type="text" name="state_code" value="STATE CODE" /> </td> <td> <h2>Ship To:</h2> <input type="text" name="recipient_name" value="RECIPIENT NAME" /> <br/> <input type="text" name="Recipient_Company_Name" value="RECIPEINT COMPANY NAME" /> <br/> <input type="text" name="Recipient_Address_Line_1" value="RECIPIENT ADDRESS LINE 1" /> <br/> <input type="text" name="Recipient_city_name" value="RECIPIENT CITY NAME" /> <br/> <input type="text" name="Recipient_postal_code" value="RECIPIENT POSTAL CODE" /> <br/> <input type="text" name="Recipient_country_code" value="GB" /> <br/> <input type="text" name="Recipient_state_code" value="RECIPIENT STATE CODE" /> <br/> <input type="text" name="Recipient_telephone" value="RECIPIENT TELEPHONE" /> <input type="submit" value="Genrate Label" /> </form>
Step 2: Now after submit the form action move to postshipping.php file. In this file check if create_xml hidden input post. Than create an XML for UPS shipping request.
<?php
if(isset($_POST['create_xml'])){
// echo "Links Data Posted";
/* All Links data from the form is now being stored in variables in string format */
$Sender_Name = $_POST['sender_name'];
$Sender_Attention_Name = $_POST['attention_name'];
$Address_Line_1 = $_POST['Address_Line_1'];
$City_Name = $_POST['city_name'];
$State_code = $_POST['state_code'];
$Country_code = $_POST['country_code'];
$Postal_code = $_POST['postal_code'];
$Rec_Attention_Name = $_POST['Attention_Name'];
$Recipient_Company_Name1 = $_POST['Recipient_Company_Name'];
$Recipient_Address_Line_1 = $_POST['Recipient_Address_Line_1'];
$Recipient_City_Name = $_POST['Recipient_city_name'];
$Recipient_State_code = $_POST['Recipient_state_code'];
$Recipient_Postal_code = $_POST['Recipient_postal_code'];
$Recipient_Country_code = $_POST['Recipient_country_code'];
$Package_Weight = $_POST['weight'];
$pickupdate=$_POST['pickup_date'];
$xmlBeg = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$xml_document = $xmlBeg;
$xml_document .= '<AccessRequest>';
$xml_document .= '<AccessLicenseNumber>';
$xml_document .= '{LICENSE}';
$xml_document .= '</AccessLicenseNumber>';
$xml_document .= '<UserId>';
$xml_document .= '{USER_ID}';
$xml_document .= '</UserId>';
$xml_document .= '<Password>';
$xml_document .= '{PASSWORD}';
$xml_document .= '</Password>';
$xml_document .= '</AccessRequest>';
$xml_document .= '<ShipmentConfirmRequest>';
$xml_document .= '<Request>';
$xml_document .= '<TransactionReference>';
$xml_document .= '<CustomerContext>';
$xml_document .= 'ups-php';
$xml_document .= '</CustomerContext>';
$xml_document .= '<XpciVersion>';
$xml_document .= '1.0001';
$xml_document .= '</XpciVersion>';
$xml_document .= '</TransactionReference>';
$xml_document .= '<RequestAction>';
$xml_document .= 'ShipConfirm';
$xml_document .= '</RequestAction>';
$xml_document .= '<RequestOption>';
$xml_document .= 'nonvalidate';
$xml_document .= '</RequestOption>';
$xml_document .= '</Request>';
$xml_document .= '<Shipment>';
$xml_document .= '<Shipper>';
$xml_document .= '<Name>';
$xml_document .= $Sender_Name;
$xml_document .= '</Name>';
$xml_document .= '<AttentionName>';
$xml_document .= $Sender_Attention_Name;
$xml_document .= '</AttentionName>';
$xml_document .= '<ShipperNumber>';
$xml_document .= 'A2E322';
$xml_document .= '</ShipperNumber>';
$xml_document .= '<Address>';
$xml_document .= '<AddressLine1>';
$xml_document .= $Address_Line_1;
$xml_document .= '</AddressLine1>';
$xml_document .= '<City>';
$xml_document .= $City_Name;
$xml_document .= '</City>';
$xml_document .= '<StateProvinceCode>';
$xml_document .= $State_code;
$xml_document .= '</StateProvinceCode>';
$xml_document .= '<CountryCode>';
$xml_document .= $Country_code;
$xml_document .= '</CountryCode>';
$xml_document .= '<PostalCode>';
$xml_document .= $Postal_code;
$xml_document .= '</PostalCode>';
$xml_document .= '</Address>';
$xml_document .= '</Shipper>';
$xml_document .= '<ShipTo>';
$xml_document .= '<CompanyName>';
$xml_document .= $Recipient_Company_Name1;
$xml_document .= '</CompanyName>';
$xml_document .= '<AttentionName>';
$xml_document .= $Rec_Attention_Name;
$xml_document .= '</AttentionName>';
$xml_document .= '<PhoneNumber>';
$xml_document .= '<StructuredPhoneNumber>';
$xml_document .= '<PhoneDialPlanNumber>';
$xml_document .= '410';
$xml_document .= '</PhoneDialPlanNumber>';
$xml_document .= '<PhoneLineNumber>';
$xml_document .= '5551212';
$xml_document .= '</PhoneLineNumber>';
$xml_document .= '<PhoneExtension>';
$xml_document .= '1234';
$xml_document .= '</PhoneExtension>';
$xml_document .= '</StructuredPhoneNumber>';
$xml_document .= '</PhoneNumber>';
$xml_document .= '<Address>';
$xml_document .= '<AddressLine1>';
$xml_document .= $Recipient_Address_Line_1;
$xml_document .= '</AddressLine1>';
$xml_document .= '<City>';
$xml_document .= $Recipient_City_Name;
$xml_document .= '</City>';
$xml_document .= '<StateProvinceCode>';
$xml_document .= $Recipient_State_code;
$xml_document .= '</StateProvinceCode>';
$xml_document .= '<CountryCode>';
$xml_document .= $Recipient_Country_code;
$xml_document .= '</CountryCode>';
$xml_document .= '<PostalCode>';
$xml_document .= $Recipient_Postal_code;
$xml_document .= '</PostalCode>';
$xml_document .= '<ResidentialAddress>';
$xml_document .= '';
$xml_document .= '</ResidentialAddress>';
$xml_document .= '</Address>';
$xml_document .= '</ShipTo>';
$xml_document .= '<Service>';
$xml_document .= '<Code>';
$xml_document .= '07';
$xml_document .= '</Code>';
$xml_document .= '<Description>';
$xml_document .= 'Worldwide Express';
$xml_document .= '</Description>';
$xml_document .= '</Service>';
$xml_document .= '<PaymentInformation>';
$xml_document .= '<Prepaid>';
$xml_document .= '<BillShipper>';
$xml_document .= '<CreditCard>';
$xml_document .= '<Type>';
$xml_document .= '06';
$xml_document .= '</Type>';
$xml_document .= '<Number>';
$xml_document .= '4111111111111111';
$xml_document .= '</Number>';
$xml_document .= '<ExpirationDate>';
$xml_document .= '011909';
$xml_document .= '</ExpirationDate>';
$xml_document .= '</CreditCard>';
$xml_document .= '</BillShipper>';
$xml_document .= '</Prepaid>';
$xml_document .= '</PaymentInformation>';
$xml_document .= '<ShipmentServiceOptions>';
$xml_document .= '<OnCallAir>';
$xml_document .= '<PickupDetails>';
$xml_document .= '<PickupDate>';
$xml_document .= $pickupdate;
$xml_document .= '</PickupDate>';
$xml_document .= '<EarliestTimeReady>';
$xml_document .= '0945';
$xml_document .= '</EarliestTimeReady>';
$xml_document .= '<LatestTimeReady>';
$xml_document .= '1445';
$xml_document .= '</LatestTimeReady>';
$xml_document .= '<ContactInfo>';
$xml_document .= '<Name>';
$xml_document .= 'JaneSmith';
$xml_document .= '</Name>';
$xml_document .= '<PhoneNumber>';
$xml_document .= '9725551234';
$xml_document .= '</PhoneNumber>';
$xml_document .= '</ContactInfo>';
$xml_document .= '</PickupDetails>';
$xml_document .= '</OnCallAir>';
$xml_document .= '</ShipmentServiceOptions>';
$xml_document .= '<Package>';
$xml_document .= '<PackagingType>';
$xml_document .= '<Code>';
$xml_document .= '02';
$xml_document .= '</Code>';
$xml_document .= '</PackagingType>';
$xml_document .= '<Dimensions>';
$xml_document .= '<UnitOfMeasurement>';
$xml_document .= '<Code>';
$xml_document .= 'CM';
$xml_document .= '</Code>';
$xml_document .= '</UnitOfMeasurement>';
$xml_document .= '<Length>';
$xml_document .= '22';
$xml_document .= '</Length>';
$xml_document .= '<Width>';
$xml_document .= '20';
$xml_document .= '</Width>';
$xml_document .= '<Height>';
$xml_document .= '18';
$xml_document .= '</Height>';
$xml_document .= '</Dimensions>';
$xml_document .= '<PackageWeight>';
$xml_document .= '<Weight>';
$xml_document .= $Package_Weight;
$xml_document .= '</Weight>';
$xml_document .= '</PackageWeight>';
$xml_document .= '</Package>';
$xml_document .= '<PackageServiceOptions>';
$xml_document .= '<InsuredValue>';
$xml_document .= '<CurrencyCode>';
$xml_document .= 'EUR';
$xml_document .= '</CurrencyCode>';
$xml_document .= '<MonetaryValue>';
$xml_document .= '149.99';
$xml_document .= '</MonetaryValue>';
$xml_document .= '</InsuredValue>';
$xml_document .= '</PackageServiceOptions>';
$xml_document .= '</Shipment>';
$xml_document .= '</ShipmentConfirmRequest>';
$xml_document .= '<LabelSpecification>';
$xml_document .= '<LabelPrintMethod>';
$xml_document .= '<Code>';
$xml_document .= 'GIF';
$xml_document .= '</Code>';
$xml_document .= '</LabelPrintMethod>';
$xml_document .= '<HTTPUserAgent>';
$xml_document .= 'Mozilla/4.5';
$xml_document .= '</HTTPUserAgent>';
$xml_document .= '<LabelImageFormat>';
$xml_document .= '<Code>';
$xml_document .= 'GIF';
$xml_document .= '</Code>';
$xml_document .= '</LabelImageFormat>';
$xml_document .= '</LabelSpecification>';
$path_dir = "ups_library/xml/";
$path_dir .= "AccessRequest.xml";
/* Data in Variables ready to be written to an XML file */
$fp = fopen($path_dir,'w');
$write = fwrite($fp,$xml_document);
/* Loading the created XML file to check contents */
$sites = simplexml_load_file("$path_dir");
}
?>In above postshipping.php new shipping information for UPS api created with the name of “Accessrequest.xml“
Step 3: Now you can use this xml data in another php file newship.php
<html>
<body>
<?php
// Require the main ups class and upsRate
require('../../clasess/class.ups.php');
require('../../clasess/class.upsShip.php');
// Get credentials from a form
$accessNumber ='Access Number';
$username = 'Username';
$password = 'Password';
// Approve the label
$approve = $_POST['approve'];
$ShipmentDigest = $_POST['ShipmentDigest'];
// If the form is filled out go get a rate from UPS
if ($accessNumber != '' && $username != '' && $password != '') {
$upsConnect = new ups("$accessNumber","$username","$password");
$upsConnect->setTemplatePath('../../xml/');
$upsConnect->setTestingMode(1); // Change this to 0 for production
$upsShip = new upsShip($upsConnect);
?>
<?php
if ($approve == 'approve shipment') {
echo $upsShip->buildShipmentAcceptXML($ShipmentDigest);
// echo $upsShip->responseXML;
$responseArray = $upsShip->responseArray();
$htmlImage = $responseArray['ShipmentAcceptResponse']['ShipmentResults']['PackageResults']['LabelImage']['GraphicImage']['VALUE'];
echo '<img src="data:image/gif;base64,'. $htmlImage. '"/>';
} else {
$var1 = $upsShip->buildRequestXML();
//echo $xmlvalue = htmlspecialchars($var1);
$xml = simplexml_load_string($var1);
//echo "<pre>";
//print_r($xml);
foreach( $xml->ShipmentDigest as $entry )
{
$ShipmentDigest1=$entry;
}
$responseArray = $upsShip->responseArray();
}
?>
<?php } ?>
<pre><?php htmlspecialchars($upsShip->xmlSent); ?></pre>
</body>
</html>
<?php
$xmlBeg1 = '<?xml version="1.0? encoding="ISO-8859-1"?>';
$xmlRequest2= $xmlBeg1;
$xmlRequest2 .= '<AccessRequest>';
$xmlRequest2 .= '<AccessLicenseNumber>';
$xmlRequest2 .= '7CC8853C00DEB722';
$xmlRequest2 .= '</AccessLicenseNumber>';
$xmlRequest2 .= '<UserId>';
$xmlRequest2 .= 'A2E322';
$xmlRequest2 .= '</UserId>';
$xmlRequest2 .= '<Password>';
$xmlRequest2 .= 'Zaanweg110';
$xmlRequest2 .= '</Password>';
$xmlRequest2 .= '</AccessRequest>';
$xmlRequest1 = '<?xml version="1.0? encoding="ISO-8859-1"?>';
$xmlRequest1 .= '<ShipmentAcceptRequest>';
$xmlRequest1 .= '<Request>';
$xmlRequest1 .= '<TransactionReference>';
$xmlRequest1 .= '<CustomerContext>';
$xmlRequest1 .= 'Customer Comment';
$xmlRequest1 .= '</CustomerContext>';
$xmlRequest1 .= '</TransactionReference>';
$xmlRequest1 .= '<RequestAction>';
$xmlRequest1 .= 'ShipAccept';
$xmlRequest1 .= '</RequestAction>';
$xmlRequest1 .= '<RequestOption>';
$xmlRequest1 .= '1';
$xmlRequest1 .= '</RequestOption>';
$xmlRequest1 .= '</Request>';
$xmlRequest1 .= '<ShipmentDigest>';
$xmlRequest1 .= $ShipmentDigest1;
$xmlRequest1 .= '</ShipmentDigest>';
$xmlRequest1 .= '</ShipmentAcceptRequest>';
$combinexml = $xmlRequest2.$xmlRequest1;
$newxmldata = htmlspecialchars($combinexml);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://wwwcie.ups.com/ups.app/xml/ShipAccept");
// uncomment the next line if you get curl error 60: error setting certificate verify locations
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// uncommenting the next line is most likely not necessary in case of error 60
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $combinexml);
curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
//if ($this->logfile) {
// error_log(“UPS REQUEST: ” . $xmlRequest . “\n”, 3, $this->logfile);
//}
$xmlResponse = curl_exec ($ch); // SHIP ACCEPT RESPONSE
//echo curl_errno($ch);
$xml = $xmlResponse;
//echo"<pre>";
//print_r($xml);
preg_match_all( "/\<ShipmentAcceptResponse\>(.*?)\<\/ShipmentAcceptResponse\>/s",$xml, $bookblocks );
foreach( $bookblocks[1] as $block )
{
preg_match_all( "/\<GraphicImage\>(.*?)\<\/GraphicImage\>/",$block, $author ); // GET LABEL
preg_match_all( "/\<TrackingNumber\>(.*?)\<\/TrackingNumber\>/",$block, $tracking ); // GET TRACKING NUMBER
//echo( $author[1][0].”\n” );
}
echo '<img src="data:image/gif;base64,'. $author[1][0]. '"/>';
?>Use this above sample code to create UPS shipping label.

Do you have any suggestion as to what the request would look like when trying to generate an international label?
Hi Matthhew,
I think request will be same, for more information please check UPS shipping documentation.
Unfortunately ups_ship_confirm doesn’t have a provision for it in their simply xml request. I do see it in the more complicated version that requires a php soap extension.
Can you send me what error you getting ? Also send me your code file on my email: [email protected]
How to find UPS LTL and UPS GFP price with API PHP?
Hi, i think you should check UPS official API documentation, All detail mentioned in it.
I check UPS official API documentation. Now I have find the LTL but not able to find GFP. How to find GFP? what is the service code for GFP?
Hi Harish,
Having an issue with the Ajaxsubmit.php. I’m getting Missing or invalid shipment digest.
Any ideas?
[ResponseStatusCode] => 0
[ResponseStatusDescription] => Failure
[Error] => SimpleXMLElement Object
(
[ErrorSeverity] => Hard
[ErrorCode] => 125000
[ErrorDescription] => Missing or invalid shipment digest.
)
Hi, I suggest you buy this script, In this, you will get ready and working UPS create Shipment code. https://www.codefixup.com/shop/ups-shipping-api-php-script/2/
I was using sandbox url with live ids, now I have updated live URLs and new error message is
Array ( [0] => Array ( [0] => Customer Comment0FailureHard125000Missing or invalid shipment digest. ) [1] => Array ( [0] => Customer Comment0FailureHard125000Missing or invalid shipment digest. ) )
Hi khurram, i have replied on your email ID. Please follow up there.