Skip to content
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
228 changes: 77 additions & 151 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,159 +1,85 @@
# UPS (United Parcel Service) PHP API

#### Version 0.1.0 (2012-08-22)

---

### Table Of Contents

1. [About the Project](#about-the-project)
2. [Features](#features)
3. [Setters & Getters](#setters--getters)
4. [Live Shipping Rates](#live-shipping-rates)
5. [Credits](#credits)
6. [License](#license)

---

### About the Project

So after searching around the Internet, it's apparent that there are no modern OOP UPS (United Parcel Service [www.ups.com](http://www.ups.com)) libraries. This library aims to provide a solid foundation for other developers to fork, contribute, and use for their own projects when interacting with the UPS API.

*While the live rates API is stable and is encouraged to be used, just know that this project is very early in development. While the goal is to support the entire API, it will take some time, especially since there are no quality examples to use. Please contribute with pull requests to speed things up!*

---

### Features

1. Consistent and extendible API
2. Live shipping rates between multiple countries
3. Live shipping rates for multiple packages (with unique weights and dimensions)
4. Framework Agnostic. Great for CodeIgniter and Laravel

---

### Setters & Getters

The setters and getters are dynamic, and included by default in the library using PHP magic method. Meaning, you can set or get any object property using a memorable and consistent syntax.

#### Setters

*Setters are used to override default property values.*

$Ups = new Ups(array(
'access_key' => 'your-access-key',
'username' => 'your-username',
'password' => 'your-password',
'account_number' => 'your-account-number',
'origin' => 'your-zipcode'
));

$Ups->set_country_code('US');
$Ups->set_shipping_type('03'); // Use the numberic code for UPS
$Ups->set_shipping_type('UPS Ground'); // OR use the service name


#### Getters

*Getters are used to retrieve property values.*

$Ups = new Ups(array(
'access_key' => 'your-access-key',
'username' => 'your-username',
'password' => 'your-password',
'account_number' => 'your-account-number',
'origin' => 'your-zipcode'
));

$country_code = $Ups->get_country_code();
$shipping_type = $Ups->get_shipping_type();

----

# Live Shipping Rates

get_rates(mixed $destination, array $packages);

**$destination** : An array containing address components or a 5 digit zipcode string.

**$packages** : An array of arrays that contain indexes and values that give the packages weight and dimension.

### How to Use

#### Step 1. Initialize the Library

// Define your origin
$origin = 'your-zipcode';

// OR use the long syntax for non-US locations
$origin = array(
'state' => 'IN',
'postal_code' => '46060',
'country_code' => 'US'
);

$auth_params = array(
'access_key' => 'your-access-key',
'username' => 'your-username',
'password' => 'your-password',
'account_number' => 'your-account-number',
'origin' => $origin
);

// PHP
$Ups = new Ups($auth_params);

// CodeIgniter
$this->CI->load->library('Ups', $auth_params);

// ExpressionEngine
$this->EE->load->library('Ups', $auth_params);

#### Step 2. Set your destination and package data.

// Height, width, depth are optional. Weight is required.
$packages = array(
array(
'height' => 12,
'width' => 12,
'depth' => 1,
'weight' => 2
),
array(
'height' => 6,
'width' => 6,
'depth' => 2,
'weight' => 1
)
);

// Define a destination with a 5 digit zip (US Only)
$destination = '33010'; // Postal code for Miami, FL

// OR use the long syntax for non-US locations
$destination = array(
'state' => 'FL',
'postal_code' => '33010',
'country_code' => 'US'
);


#### Step 3. Get the live rates

// PHP
$rate = $Ups->get_rate($destination, $packages);

// CodeIgniter
$rate = $this->CI->ups->get_rate($destination, $packages);

// ExpressionEngine
$rate = $this->EE->ups->get_rate($destination, $packages);

---
#### Version 0.1.0 (2015-05-23)


### Install

I recommend to use composer for installing. Here is a sample of composer.json file to include UPS library:

{
"name": "StudyProject",
"repositories": [
{
"type": "git",
"url": "https://github.com/KoulSlou/UPS.git"
}
],
"require": {
"KoulSlou/UPS":"dev-master"
}
}


### How To Use

First you need to initialize Ups object with your account credentials:


$auth_params = array(
'access_key' => '1234567890',
'username' => 'JohnSmith',
'password' => '*******',
'account_number' => '123456'
);

$Ups = new Ups($auth_params);

Then you need to specify origin and destination. You can provide only zipcode string for US address, for other countries
provide array with address information.


$destination = array(
'state' => 'FL',
'postal_code' => '33010',
'country_code' => 'US',
'company' => 'test',
'fax' => '123'
);


$origin = '32548'

Now define information about packages your going to send:

// Height, width, depth are optional. Weight is required.
$packages = array(
array(
'height' => 12,
'width' => 12,
'depth' => 1,
'weight' => 2
),
array(
'height' => 6,
'width' => 6,
'depth' => 2,
'weight' => 1
)
);

To get rates call get_rate() function:

$rates = $Ups->rates->get_rate($packages);

You will receive object of Ups_Live_Rates. In rates property you will have array of available shipping methods (array of
Ups_Rate objects)



### Credits

The following URL contains the only library I could find that was of any use was for interacting with UPS. I started from this code, and removed all the hardcoded variables, added a proper setters/getters, and made the packaging system much more robust.[CodeIgniter UPS Rate Tool](https://github.com/EllisLab/CodeIgniter/wiki/UPS-Rate-Tool)
Here is a link to the repository that I used: https://github.com/objectivehtml/UPS

---

Expand Down
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name":"KoulSlou/UPS",
"autoload": {
"files": [
"libraries/Ups/Ups_Base.php",
"libraries/Ups/Ups_Base_Response.php",
"libraries/Ups/Ups.php",
"libraries/Ups/Ups_Live_Rates.php",
"libraries/Ups/Ups_Rate.php"
]
}
}
5 changes: 1 addition & 4 deletions libraries/Ups/Ups.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

require_once 'Ups_Base.php';
require_once 'Ups_Live_Rates.php';

class Ups extends Base_Ups {

private $available_apis = array(
Expand Down Expand Up @@ -42,4 +39,4 @@ public function load($apis = FALSE)
$this->$obj = new $class($params);
}
}
}
}
4 changes: 1 addition & 3 deletions libraries/Ups/Ups_Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* @build 20120823
*/

require_once 'Ups_Base_Response.php';

abstract class Base_Ups
{
/**
Expand Down Expand Up @@ -180,4 +178,4 @@ protected function url($endpoint)
{
return rtrim($this->base_url, '/') . '/' . ltrim($endpoint, '/');
}
}
}
13 changes: 13 additions & 0 deletions libraries/Ups/Ups_Base_Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ public function __construct($data = array())
}
}
}


public function __set($name, $value)
{


if(property_exists($this, $name))
$this->{$name} = $value;
else if(property_exists($this, ("_".$name)))
$this->{"_".$name} = $value;
else
throw new Exception("Trying to set unexisting property ".$name);
}


/**
Expand Down
Loading