Code Monkey home page Code Monkey logo

laravel-usps's Introduction

Laravel-Usps

This package provides a very simple wrapper for the United States Postal Service API. Currently, this package only provides address validation features, but will soon comprise all features offered by the USPS API.

Prerequisites

Be sure to register at www.usps.com/webtools/ to receive your unique user ID from the United States Postal Service. This user ID is required to use this package.

Installation

composer require webreinvent/laravel-usps

Configuration

There are three important configurations.

  1. Your USPS user ID:

    • If you have not received your USPS user ID, follow the link in the prerequisites section to register with the United States Postal Service. It is required to use this package.
  2. Whether you want SSL verification enabled for API requests:

    • This setting is set to true by default for security reasons. You can override this behavior by setting the verrifyssl config setting to false. Do this at your own risk.
  3. Which environment you are working in:

    • The options are 'local' and 'production' which tell the package which API url to use, testing or production respectively. If no configuration is found for env, it will default to the environment recognized by laravel. This setting takes precedence over APP_ENV from your .env file.

We recommend placing all configuration settings in your .env file and use Laravel's env() helper function to access these values.

In config/services.php add these three settings.

'usps' => [

    'userid' => env('USPS_USER_ID'), // string
    'verifyssl' => env('USPS_VERIFY_SSL'), // bool
    'env' => env('USPS_ENV') //string
];

Usage

The current features offered by this package are:

Address Validation

The Address class handles creating and formatting address data. Pass the constructor an associative array of address details. Array keys are case-sensitive. Below is an example of creating an address and making an api request for validation.

use WebReinvent\Usps\Address;

$address = new Address([
    'Address2' => '6406 Ivy Lane',
    'City' => 'Greenbelt',
    'State' => 'MD',
    'Zip5' => 20770
]);

$response = $address->validate();

The USPS api supports up to 5 address validations per request. If you need to validate more than one address at a time, pass an array of addresses to the Address constructor.

use WebReinvent\Usps\Address;

$address1 = [
    'Address2' => '6406 Ivy Lane',
    'City' => 'Greenbelt',
    'State' => 'MD',
    'Zip5' => 20770
];

$address2 = [
    'Address2' => '2185 Sandy Drive',
    'City' => 'Franklin',
    'State' => 'VA',
    'Zip5' => 32050
];

$addresses = new Address([$address1, $address2]);

$response = $addresses->validate();

The response will contain the corrected address, or an error if not enough information was given or the address does not exists.

Response Formatting

You can specify the format of the response by passing an optional, case-insensitive parameter to the validate method. The default format is an associative array.

$address = new Address([/* address info */ ]);

$json = $address->validate('json');
$object = $address->validate('object');
$string = $address->validate('string');

laravel-usps's People

Contributors

aknud avatar ctwillie avatar themodernpk avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

aknud

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.