By: Prem Tiwari | Last Updated: | In: Freebies, PHP Tutorial
United Parcel Service of North America, Inc., typically referred to and branded as UPS (stylized as ups), is one of the largest shipment and logistics companies in the world.
UPS is known for its brown delivery trucks and uniforms, hence the company nickname “Brown”. UPS also operates its own airline and air cargo delivery service.
This library uses PHP 5.3+.
To use the UPS API, you have to request an access key from UPS. For every request, you will have to provide the Access Key, your UPS User ID, and Password.
The Calculate Time and Cost application help you find delivery dates and times for all available UPS package and freight services in over 200 countries and territories. You can also use the application as a shipping calculator to determine shipping costs for a range of UPS services in many countries served by UPS.
The Calculate Time and Cost application is located on the Shipping tab of ups.com.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
<?php namespace Ups; use DOMDocument; use SimpleXMLElement; use Exception; use stdClass; abstract class Ups { const PRODUCTION_BASE_URL = 'https://onlinetools.ups.com/ups.app/xml'; const INTEGRATION_BASE_URL = 'https://wwwcie.ups.com/ups.app/xml'; /** * @var string */ protected $accessKey; /** * @var string */ protected $userId; /** * @var string */ protected $password; /** * @var string * @deprecated */ protected $productionBaseUrl = 'https://onlinetools.ups.com/ups.app/xml'; /** * @var string * @deprecated */ protected $integrationBaseUrl = 'https://wwwcie.ups.com/ups.app/xml'; /** * @var bool */ protected $useIntegration = false; /** * @var string */ protected $context; /** * @deprecated */ public $response; /** * Constructor * * @param string|null $accessKey UPS License Access Key * @param string|null $userId UPS User ID * @param string|null $password UPS User Password * @param bool $useIntegration Determine if we should use production or CIE URLs. */ public function __construct($accessKey = null, $userId = null, $password = null, $useIntegration = false) { $this->accessKey = $accessKey; $this->userId = $userId; $this->password = $password; $this->useIntegration = $useIntegration; } /** * Sets the transaction / context value * * @param string $context The transaction "guidlikesubstance" value */ public function setContext($context) { $this->context = $context; } /** * Format a Unix timestamp or a date time with a Y-m-d H:i:s format into a YYYYMMDDHHmmss format required by UPS. * * @param string * @return string */ public function formatDateTime($timestamp) { if (!is_numeric($timestamp)) { $timestamp = strtotime($timestamp); } return date('YmdHis', $timestamp); } /** * Create the access request * * @return string */ protected function createAccess() { $xml = new DOMDocument(); $xml->formatOutput = true; // Create the AccessRequest element $accessRequest = $xml->appendChild($xml->createElement("AccessRequest")); $accessRequest->setAttribute('xml:lang', 'en-US'); $accessRequest->appendChild($xml->createElement("AccessLicenseNumber", $this->accessKey)); $accessRequest->appendChild($xml->createElement("UserId", $this->userId)); $accessRequest->appendChild($xml->createElement("Password", $this->password)); return $xml->saveXML(); } /** * Creates the TransactionReference node for a request * * @return DomDocument */ protected function createTransactionNode() { $xml = new DOMDocument; $xml->formatOutput = true; $trxRef = $xml->appendChild($xml->createElement('TransactionReference')); if (null !== $this->context) { $trxRef->appendChild($xml->createElement('CustomerContext', $this->context)); } return $trxRef->cloneNode(true); } /** * Send request to UPS * * @param string $access The access request xml * @param string $request The request xml * @param string $endpointurl The UPS API Endpoint URL * @return SimpleXMLElement * @throws Exception * @deprecated Untestable */ protected function request($access, $request, $endpointurl) { $requestInstance = new Request; $response = $requestInstance->request($access, $request, $endpointurl); if ($response->getResponse() instanceof SimpleXMLElement) { $this->response = $response->getResponse(); return $response->getResponse(); } throw new Exception("Failure: Response is invalid."); } /** * Convert XMLSimpleObject to stdClass object * * @param SimpleXMLElement $xmlObject * @return stdClass */ protected function convertXmlObject(SimpleXMLElement $xmlObject) { return json_decode(json_encode($xmlObject)); } /** * Compiles the final endpoint URL for the request. * * @param string $segment The URL segment to build in to the endpoint * @return string */ protected function compileEndpointUrl($segment) { $base = ($this->useIntegration ? $this->integrationBaseUrl : $this->productionBaseUrl); return $base . $segment; } } |
Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies. View all posts by Prem Tiwari
Technology, Tip and tricks, ups shipping, ups shipping rates, Web development