From 49fa8ffd0c0d9375efa14d9a3a34650aa40d5bf2 Mon Sep 17 00:00:00 2001 From: Doug Bierer Date: Sat, 26 Aug 2023 15:10:16 +0700 Subject: [PATCH 1/2] Updating API --- README.md | 15 +++ demos/address_verify.php | 12 ++- demos/citystatelookup.php | 11 +- demos/domestic_rate.php | 8 +- demos/firstclassservicestandard.php | 9 +- demos/international_express_label.php | 20 ++-- demos/open_label.php | 13 ++- demos/priority_label.php | 13 ++- demos/servicedeliverycalculator.php | 10 +- demos/trackconfirm.php | 12 ++- demos/zipcodelookup.php | 8 +- index.php | 40 +++++++ src/USPS/USPSBase.php | 25 +++-- src/USPS/USPSRate.php | 146 ------------------------- src/USPS/USPSRatePackage.php | 149 ++++++++++++++++++++++++++ 15 files changed, 297 insertions(+), 194 deletions(-) create mode 100644 index.php create mode 100644 src/USPS/USPSRatePackage.php diff --git a/README.md b/README.md index 46190c0..8118a85 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,9 @@ This wrapper allows you to perform some basic calls to the USPS api. Some of the - Service Delivery Calculator - Confirm Tracking +Be sure to read through the USPS develeloper documentation first! +* https://www.usps.com/business/web-tools-apis/general-api-developer-guide.htm + Requirements ============ @@ -26,6 +29,18 @@ Examples ======= Please check the 'demos' directory for usage examples on the various types of api calls and actions you can perform. +To run the demos you'll need to refresh Composer autoloading: +``` +wget https://getcomposer.org/composer.phar +php composer.phar dump-autoload +``` + +To run via the built-in PHP web server: +* Run this command from the root directory of this project: +``` +php -S localhost:8888 +``` +* From your browser: `http://localhost:8888` Authors diff --git a/demos/address_verify.php b/demos/address_verify.php index e40ebd9..08fcd0f 100644 --- a/demos/address_verify.php +++ b/demos/address_verify.php @@ -1,12 +1,16 @@ setTestMode(true); +$verify->setTestMode(true); // Create new address object and assign the properties // apartently the order you assign them is important so make sure diff --git a/demos/citystatelookup.php b/demos/citystatelookup.php index 3cf178d..6ab10cc 100644 --- a/demos/citystatelookup.php +++ b/demos/citystatelookup.php @@ -1,12 +1,15 @@ setTestMode(true); +$verify->setTestMode(true); // Add the zip code we want to lookup the city and state $verify->addZipCode('91730'); diff --git a/demos/domestic_rate.php b/demos/domestic_rate.php index 288bdfc..40a88ff 100644 --- a/demos/domestic_rate.php +++ b/demos/domestic_rate.php @@ -1,12 +1,14 @@ setTestMode(true); +$rate->setTestMode(true); // Create new package object and assign the properties // apartently the order you assign them is important so make sure diff --git a/demos/firstclassservicestandard.php b/demos/firstclassservicestandard.php index c8fd590..887532e 100644 --- a/demos/firstclassservicestandard.php +++ b/demos/firstclassservicestandard.php @@ -1,9 +1,12 @@ setTestMode(true); diff --git a/demos/international_express_label.php b/demos/international_express_label.php index f391f3e..af6197c 100644 --- a/demos/international_express_label.php +++ b/demos/international_express_label.php @@ -1,18 +1,18 @@ setApiVersion('ExpressMailIntl'); - -// PriorityMailIntl -$label->setApiVersion('PriorityMailIntl'); - -// FirstClassMailIntl -$label->setApiVersion('FirstClassMailIntl'); +$label->setApiVersion($version); // During test mode this seems not to always work as expected $label->setTestMode(true); diff --git a/demos/open_label.php b/demos/open_label.php index 577d8a0..f81f076 100644 --- a/demos/open_label.php +++ b/demos/open_label.php @@ -1,10 +1,17 @@ setTestMode(true); $label->setFromAddress('John', 'Doe', '', '5161 Lankershim Blvd', 'North Hollywood', 'CA', '91601', '# 204'); $label->setToAddress('Vincent Gabriel', '5440 Tujunga Ave', 'North Hollywood', 'CA', '91601', '707'); diff --git a/demos/priority_label.php b/demos/priority_label.php index f9686fc..012c3e5 100644 --- a/demos/priority_label.php +++ b/demos/priority_label.php @@ -1,9 +1,14 @@ setTestMode(true); @@ -29,7 +34,7 @@ //echo "\n Confirmation:" . $label->getConfirmationNumber(); $label = $label->getLabelContents(); - + if($label) { $contents = base64_decode($label); header('Content-type: application/pdf'); diff --git a/demos/servicedeliverycalculator.php b/demos/servicedeliverycalculator.php index 518e793..392a961 100644 --- a/demos/servicedeliverycalculator.php +++ b/demos/servicedeliverycalculator.php @@ -1,9 +1,13 @@ setTestMode(true); diff --git a/demos/trackconfirm.php b/demos/trackconfirm.php index 327c6ac..a2a6239 100644 --- a/demos/trackconfirm.php +++ b/demos/trackconfirm.php @@ -1,9 +1,13 @@ setTestMode(true); @@ -22,4 +26,4 @@ echo 'Error: ' . $tracking->getErrorMessage(); } -?> \ No newline at end of file +?> diff --git a/demos/zipcodelookup.php b/demos/zipcodelookup.php index f3301cc..041852a 100644 --- a/demos/zipcodelookup.php +++ b/demos/zipcodelookup.php @@ -1,12 +1,14 @@ setTestMode(true); +$verify->setTestMode(true); // Create new address object and assign the properties // apartently the order you assign them is important so make sure diff --git a/index.php b/index.php new file mode 100644 index 0000000..f9de65c --- /dev/null +++ b/index.php @@ -0,0 +1,40 @@ +'; +$list = new RecursiveDirectoryIterator(__DIR__ . '/demos'); +foreach ($list as $name => $obj) { + $html .= ($obj->isFile()) + ? '
  • ' . basename($name) . '
  • ' + : ''; +} +$html .= ''; +?> + + + + +USPS PHP API Test + + + +
    +USPS Username:   +USPS Password:   + +
    + + + + diff --git a/src/USPS/USPSBase.php b/src/USPS/USPSBase.php index 9bfd323..133b6d4 100644 --- a/src/USPS/USPSBase.php +++ b/src/USPS/USPSBase.php @@ -11,13 +11,17 @@ * @author Vincent Gabriel */ class USPSBase { - const LIVE_API_URL = 'http://production.shippingapis.com/ShippingAPI.dll'; - const TEST_API_URL = 'http://production.shippingapis.com/ShippingAPITest.dll'; + const LIVE_API_URL = 'https://stg-secure.shippingapis.com/shippingapi.dll'; + const TEST_API_URL = 'https://secure.shippingapis.com/shippingapi.dll'; /** * @var string - the usps username provided by the usps website */ protected $username = ''; + /** + * @var string - the usps password provided by the usps website + */ + protected $password = ''; /** * the error code if one exists * @var integer @@ -94,9 +98,11 @@ class USPSBase { /** * Constructor * @param string $username - the usps api username + * @param string $password - the usps api password */ - public function __construct($username='') { + public function __construct($username='', $password='') { $this->username = $username; + $this->password = $password; } /** * set the usps api username we are going to user @@ -201,10 +207,15 @@ public function getEndpoint() { */ protected function getXMLString() { // Add in the defaults - $postFields = array( - '@attributes' => array('USERID' => $this->username), - ); - + if (!empty($this->password)) { + $postFields = array( + '@attributes' => array('USERID' => $this->username, 'PASSWORD' => $this->password), + ); + } else { + $postFields = array( + '@attributes' => array('USERID' => $this->username), + ); + } // Add in the sub class data $postFields = array_merge($postFields, $this->getPostFields()); diff --git a/src/USPS/USPSRate.php b/src/USPS/USPSRate.php index d6bd432..a9cf60e 100644 --- a/src/USPS/USPSRate.php +++ b/src/USPS/USPSRate.php @@ -59,149 +59,3 @@ public function addPackage(USPSRatePackage $data, $id=null) { $this->packages['Package'][] = array_merge(array('@attributes' => array('ID' => $packageId)), $data->getPackageInfo()); } } -/** - * USPS Rate Package - * used by the ups rate class to create packages represented as objects - * @since 1.0 - * @author Vincent Gabriel - */ -class USPSRatePackage extends USPSRate { - /** - * @var array - list of all packages added so far - */ - protected $packageInfo = array(); - /** - * Services constants - */ - const SERVICE_FIRST_CLASS = 'FIRST CLASS'; - const SERVICE_FIRST_CLASS_COMMERCIAL = 'FIRST CLASS COMMERCIAL'; - const SERVICE_FIRST_CLASS_HFP_COMMERCIAL = 'FIRST CLASS HFP COMMERCIAL'; - const SERVICE_PRIORITY = 'PRIORITY'; - const SERVICE_PRIORITY_COMMERCIAL = 'PRIORITY COMMERCIAL'; - const SERVICE_PRIORITY_HFP_COMMERCIAL = 'PRIORITY HFP COMMERCIAL'; - const SERVICE_EXPRESS = 'EXPRESS'; - const SERVICE_EXPRESS_COMMERCIAL = 'EXPRESS COMMERCIAL'; - const SERVICE_EXPRESS_SH = 'EXPRESS SH'; - const SERVICE_EXPRESS_SH_COMMERCIAL = 'EXPRESS SH COMMERCIAL'; - const SERVICE_EXPRESS_HFP = 'EXPRESS HFP'; - const SERVICE_EXPRESS_HFP_COMMERCIAL = 'EXPRESS HFP COMMERCIAL'; - const SERVICE_PARCEL = 'PARCEL'; - const SERVICE_MEDIA = 'MEDIA'; - const SERVICE_LIBRARY = 'LIBRARY'; - const SERVICE_ALL = 'ALL'; - const SERVICE_ONLINE = 'ONLINE'; - /** - * First class mail type - * required when you use one of the first class services - */ - const MAIL_TYPE_LETTER = 'LETTER'; - const MAIL_TYPE_FLAT = 'FLAT'; - const MAIL_TYPE_PARCEL = 'PARCEL'; - const MAIL_TYPE_POSTCARD = 'POSTCARD'; - const MAIL_TYPE_PACKAGE_SERVICE = 'PACKAGE SERVICE'; - /** - * Container constants - */ - const CONTAINER_VARIABLE = 'VARIABLE'; - const CONTAINER_FLAT_RATE_ENVELOPE = 'FLAT RATE ENVELOPE'; - const CONTAINER_PADDED_FLAT_RATE_ENVELOPE = 'PADDED FLAT RATE ENVELOPE'; - const CONTAINER_LEGAL_FLAT_RATE_ENVELOPE = 'LEGAL FLAT RATE ENVELOPE'; - const CONTAINER_SM_FLAT_RATE_ENVELOPE = 'SM FLAT RATE ENVELOPE'; - const CONTAINER_WINDOW_FLAT_RATE_ENVELOPE = 'WINDOW FLAT RATE ENVELOPE'; - const CONTAINER_GIFT_CARD_FLAT_RATE_ENVELOPE = 'GIFT CARD FLAT RATE ENVELOPE'; - const CONTAINER_FLAT_RATE_BOX = 'FLAT RATE BOX'; - const CONTAINER_SM_FLAT_RATE_BOX = 'SM FLAT RATE BOX'; - const CONTAINER_MD_FLAT_RATE_BOX = 'MD FLAT RATE BOX'; - const CONTAINER_LG_FLAT_RATE_BOX = 'LG FLAT RATE BOX'; - const CONTAINER_REGIONALRATEBOXA = 'REGIONALRATEBOXA'; - const CONTAINER_REGIONALRATEBOXB = 'REGIONALRATEBOXB'; - const CONTAINER_REGIONALRATEBOXC = 'REGIONALRATEBOXC'; - const CONTAINER_RECTANGULAR = 'RECTANGULAR'; - const CONTAINER_NONRECTANGULAR = 'NONRECTANGULAR'; - /** - * Size constants - */ - const SIZE_LARGE = 'LARGE'; - const SIZE_REGULAR = 'REGULAR'; - /** - * Set the service property - * @param string|int $value - * @return object USPSRatePackage - */ - public function setService($value) { - return $this->setField('Service', $value); - } - /** - * Set the first class mail type property - * @param string|int $value - * @return object USPSRatePackage - */ - public function setFirstClassMailType($value) { - return $this->setField('FirstClassMailType', $value); - } - /** - * Set the zip origin property - * @param string|int $value - * @return object USPSRatePackage - */ - public function setZipOrigination($value) { - return $this->setField('ZipOrigination', $value); - } - /** - * Set the zip destination property - * @param string|int $value - * @return object USPSRatePackage - */ - public function setZipDestination($value) { - return $this->setField('ZipDestination', $value); - } - /** - * Set the pounds property - * @param string|int $value - * @return object USPSRatePackage - */ - public function setPounds($value) { - return $this->setField('Pounds', $value); - } - /** - * Set the ounces property - * @param string|int $value - * @return object USPSRatePackage - */ - public function setOunces($value) { - return $this->setField('Ounces', $value); - } - /** - * Set the container property - * @param string|int $value - * @return object USPSRatePackage - */ - public function setContainer($value) { - return $this->setField('Container', $value); - } - /** - * Set the size property - * @param string|int $value - * @return object USPSRatePackage - */ - public function setSize($value) { - return $this->setField('Size', $value); - } - /** - * Add an element to the stack - * @param string|int $key - * @param string|int $value - * @return object USPSAddress - */ - public function setField($key, $value) { - $this->packageInfo[ ucwords($key) ] = $value; - return $this; - } - /** - * Returns a list of all the info we gathered so far in the current package object - * @return array - */ - public function getPackageInfo() { - return $this->packageInfo; - } -} diff --git a/src/USPS/USPSRatePackage.php b/src/USPS/USPSRatePackage.php new file mode 100644 index 0000000..d1bad1c --- /dev/null +++ b/src/USPS/USPSRatePackage.php @@ -0,0 +1,149 @@ +setField('Service', $value); + } + /** + * Set the first class mail type property + * @param string|int $value + * @return object USPSRatePackage + */ + public function setFirstClassMailType($value) { + return $this->setField('FirstClassMailType', $value); + } + /** + * Set the zip origin property + * @param string|int $value + * @return object USPSRatePackage + */ + public function setZipOrigination($value) { + return $this->setField('ZipOrigination', $value); + } + /** + * Set the zip destination property + * @param string|int $value + * @return object USPSRatePackage + */ + public function setZipDestination($value) { + return $this->setField('ZipDestination', $value); + } + /** + * Set the pounds property + * @param string|int $value + * @return object USPSRatePackage + */ + public function setPounds($value) { + return $this->setField('Pounds', $value); + } + /** + * Set the ounces property + * @param string|int $value + * @return object USPSRatePackage + */ + public function setOunces($value) { + return $this->setField('Ounces', $value); + } + /** + * Set the container property + * @param string|int $value + * @return object USPSRatePackage + */ + public function setContainer($value) { + return $this->setField('Container', $value); + } + /** + * Set the size property + * @param string|int $value + * @return object USPSRatePackage + */ + public function setSize($value) { + return $this->setField('Size', $value); + } + /** + * Add an element to the stack + * @param string|int $key + * @param string|int $value + * @return object USPSAddress + */ + public function setField($key, $value) { + $this->packageInfo[ ucwords($key) ] = $value; + return $this; + } + /** + * Returns a list of all the info we gathered so far in the current package object + * @return array + */ + public function getPackageInfo() { + return $this->packageInfo; + } +} From c13893f9f0785f06597bac39f8b38821c36ebd85 Mon Sep 17 00:00:00 2001 From: Doug Bierer Date: Sat, 26 Aug 2023 15:18:18 +0700 Subject: [PATCH 2/2] Updating API --- demos/citystatelookup.php | 5 +++-- demos/domestic_rate.php | 6 ++++-- demos/zipcodelookup.php | 10 ++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/demos/citystatelookup.php b/demos/citystatelookup.php index 6ab10cc..5f74b14 100644 --- a/demos/citystatelookup.php +++ b/demos/citystatelookup.php @@ -1,12 +1,13 @@ setTestMode(true); diff --git a/demos/domestic_rate.php b/demos/domestic_rate.php index 40a88ff..07002a8 100644 --- a/demos/domestic_rate.php +++ b/demos/domestic_rate.php @@ -1,11 +1,13 @@ setTestMode(true); diff --git a/demos/zipcodelookup.php b/demos/zipcodelookup.php index 041852a..57d0c62 100644 --- a/demos/zipcodelookup.php +++ b/demos/zipcodelookup.php @@ -1,14 +1,16 @@ setTestMode(true); +$zipcode->setTestMode(true); // Create new address object and assign the properties // apartently the order you assign them is important so make sure