Skip to content

Split USPSRatePackage and USPSRate + Rewrote Demos #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
============
Expand All @@ -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
Expand Down
12 changes: 8 additions & 4 deletions demos/address_verify.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php
// USAGE: address_verify.php USPS_USER [USPS_PASSWORD]
// NOTE: Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\{USPSAddress,USPSAddressVerify};

// Load the class
require_once('../USPSAddressVerify.php');
// Initiate and set the username provided from usps
$verify = new USPSAddressVerify('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$pass = $argv[2] ?? $_GET['password'] ?? '';
$verify = new USPSAddressVerify($user, $pass);

// During test mode this seems not to always work as expected
//$verify->setTestMode(true);
$verify->setTestMode(true);

// Create new address object and assign the properties
// apartently the order you assign them is important so make sure
Expand Down
12 changes: 8 additions & 4 deletions demos/citystatelookup.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php
// Load the class
require_once('../USPSCityStateLookup.php');
// USAGE: citystatelookup.php USPS_USER [USPS_PASSWORD]
// NOTE: Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\USPSCityStateLookup;

// Initiate and set the username provided from usps
$verify = new USPSCityStateLookup('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$pass = $argv[2] ?? $_GET['password'] ?? '';
$verify = new USPSCityStateLookup($user, $pass);

// During test mode this seems not to always work as expected
//$verify->setTestMode(true);
$verify->setTestMode(true);

// Add the zip code we want to lookup the city and state
$verify->addZipCode('91730');
Expand Down
12 changes: 8 additions & 4 deletions demos/domestic_rate.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php
// Load the class
require_once('../USPSRate.php');
// USAGE: domestic_rate.php USPS_USER [USPS_PASSWORD]
// NOTE: Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\{USPSRate,USPSRatePackage};

// Initiate and set the username provided from usps
$rate = new USPSRate('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$pass = $argv[2] ?? $_GET['password'] ?? '';
$rate = new USPSRate($user, $pass);

// During test mode this seems not to always work as expected
//$rate->setTestMode(true);
$rate->setTestMode(true);

// Create new package object and assign the properties
// apartently the order you assign them is important so make sure
Expand Down
9 changes: 6 additions & 3 deletions demos/firstclassservicestandard.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php
// Load the class
require_once('../USPSFirstClassServiceStandards.php');
// USAGE: firstclassservicestandard.php USPS_USER
// NOTE: Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\USPSFirstClassServiceStandards;

// Initiate and set the username provided from usps.
$delivery = new USPSFirstClassServiceStandards('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$delivery = new USPSFirstClassServiceStandards($user);

// During test mode this seems not to always work as expected.
$delivery->setTestMode(true);
Expand Down
20 changes: 10 additions & 10 deletions demos/international_express_label.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php
// USAGE: international_express_label.php USPS_USER USPS_PASSWORD ExpressMailIntl|PriorityMailIntl|FirstClassMailIntl
// NOTE: APIs that return printed labels or barcodes become available after applying for advanced permissions from the USPS APIs Functional Team.
// Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\USPSInternationalLabel;

// Load the class
require_once('../USPSInternationalLabel.php');
// Initiate and set the username provided from usps
$label = new USPSInternationalLabel('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$pass = $argv[2] ?? $_GET['password'] ?? '';
$version = $argv[3] ?? $_GET['version'] ?? 'ExpressMailIntl';
$label = new USPSInternationalLabel($user, $pass);

// Express by default
$label->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);
Expand Down
13 changes: 10 additions & 3 deletions demos/open_label.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<?php
// USAGE: open_label.php USPS_USER USPS_PASSWORD
// NOTE: APIs that return printed labels or barcodes become available after applying for advanced permissions from the USPS APIs Functional Team.
// Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\USPSOpenDistributeLabel;

// Load the class
require_once('../USPSOpenDistributeLabel.php');
// Initiate and set the username provided from usps
$label = new USPSOpenDistributeLabel('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$pass = $argv[2] ?? $_GET['password'] ?? '';
$label = new USPSOpenDistributeLabel($user, $pass);

// During test mode this seems not to always work as expected
$label->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');
Expand Down
13 changes: 9 additions & 4 deletions demos/priority_label.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php
// USAGE: priority_label.php USPS_USER USPS_PASSWORD
// NOTE: APIs that return printed labels or barcodes become available after applying for advanced permissions from the USPS APIs Functional Team.
// Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\USPSPriorityLabel;

// Load the class
require_once('../USPSPriorityLabel.php');
// Initiate and set the username provided from usps
$label = new USPSPriorityLabel('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$pass = $argv[2] ?? $_GET['password'] ?? '';
$label = new USPSPriorityLabel($user, $pass);

// During test mode this seems not to always work as expected
$label->setTestMode(true);
Expand All @@ -29,7 +34,7 @@
//echo "\n Confirmation:" . $label->getConfirmationNumber();

$label = $label->getLabelContents();

if($label) {
$contents = base64_decode($label);
header('Content-type: application/pdf');
Expand Down
10 changes: 7 additions & 3 deletions demos/servicedeliverycalculator.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php
// Load the class
require_once('../USPSServiceDeliveryCalculator.php');
// USAGE: servicedeliverycalculator.php USPS_USER USPS_PASSWORD
// NOTE: Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\USPSServiceDeliveryCalculator;

// Initiate and set the username provided from usps
$delivery = new USPSServiceDeliveryCalculator('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$pass = $argv[2] ?? $_GET['password'] ?? '';
$delivery = new USPSServiceDeliveryCalculator($user, $pass);

// During test mode this seems not to always work as expected
$delivery->setTestMode(true);
Expand Down
12 changes: 8 additions & 4 deletions demos/trackconfirm.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php
// Load the class
require_once('../USPSTrackConfirm.php');
// USAGE: trackconfirm.php USPS_USER USPS_PASSWORD
// NOTE: Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\USPSTrackConfirm;

// Initiate and set the username provided from usps
$tracking = new USPSTrackConfirm('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$pass = $argv[2] ?? $_GET['password'] ?? '';
$tracking = new USPSTrackConfirm($user, $pass);

// During test mode this seems not to always work as expected
$tracking->setTestMode(true);
Expand All @@ -22,4 +26,4 @@
echo 'Error: ' . $tracking->getErrorMessage();
}

?>
?>
12 changes: 8 additions & 4 deletions demos/zipcodelookup.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php
// Load the class
require_once('../USPSZipCodeLookup.php');
// USAGE: zipcodelookup.php USPS_USER [USPS_PASSWORD]
// NOTE: Be sure to run composer dump-autoload to update autoload file mapping
require __DIR__ . '/../vendor/autoload.php';
use USPS\{USPSZipCodeLookup,USPSAddress};

// Initiate and set the username provided from usps
$zipcode = new USPSZipCodeLookup('xxxx');
$user = $argv[1] ?? $_GET['user'] ?? 'xxx';
$pass = $argv[2] ?? $_GET['password'] ?? '';
$zipcode = new USPSZipCodeLookup($user, $pass);

// During test mode this seems not to always work as expected
//$verify->setTestMode(true);
$zipcode->setTestMode(true);

// Create new address object and assign the properties
// apartently the order you assign them is important so make sure
Expand Down
40 changes: 40 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
// use this to run the demo programs via the built-in PHP web server
// to run, from the project root run this command:
// php -S localhost:8888
// then, from your browser: http://localhost:8888
session_start();
if (!empty($_GET)) {
$user = (!empty($_GET['user'])) ? strip_tags($_GET['user']) : '';
$password = (!empty($_GET['password'])) ? strip_tags($_GET['password']) : '';
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
}
$user = $_SESSION['user'] ?? '';
$password = $_SESSION['password'] ?? '';
$html = '<ul>';
$list = new RecursiveDirectoryIterator(__DIR__ . '/demos');
foreach ($list as $name => $obj) {
$html .= ($obj->isFile())
? '<li><a target="_blank" href="/demos/' . basename($name) . '?user=' . $user . '&password=' . $password . '">' . basename($name) . '</a></li>'
: '';
}
$html .= '</ul>';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>USPS PHP API Test</title>
<meta name="generator" content="Geany 1.38" />
</head>
<body>
<form method="get">
USPS Username: <input type="text" name="user" value="<?= htmlspecialchars($user); ?>" />&nbsp;
USPS Password: <input type="text" name="password" value="<?= htmlspecialchars($password); ?>" />&nbsp;
<input type="submit" name="submit" value="Update" />
</form>
<?= $html; ?>
</body>
</html>

25 changes: 18 additions & 7 deletions src/USPS/USPSBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());

Expand Down
Loading