Blog

Feb
25
2012

Australia Post API with PHP

This is a quick post to show people how to use the Australia Post API with PHP, I had a hard time getting this working as the documentation is good but very verbose and there are not too many examples for PHP out there. Anyway its fairly simple once you know how.

Requirements

First I would recommend doing this in OOP style so I will start by creating a class and adding two instance variables.

class Shipping
{
	private $api = 'https://auspost.com.au/api/';
	private $auth_key = 'XXXXXXXXXXXXXXXXX';
}

The First is the BASE url to the API and the second is the API Key you obtained from Australia Post, replace the X’s with your key.

Next we will create a method for fetching data from the API.

class Shipping
{
	private $api = 'https://auspost.com.au/api/';
	private $auth_key = 'XXXXXXXXXXXXXXXXX';
 
        public function getRemoteData($url)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		  'Auth-Key: ' . $this->auth_key
		));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$contents = curl_exec ($ch);
		curl_close ($ch);
		return json_decode($contents,true);
	}
}

This gets the remote data requested and returns it as an array of data.

We now should add some constants to our class for the limits that Australia Post enforces.

and a few methods for fetching the data.

class Shipping
{
	private $api = 'https://auspost.com.au/api/';
	private $auth_key = 'XXXXXXXXXXXXXXXXX';
        const MAX_HEIGHT = 35; //only applies if same as width
	const MAX_WIDTH = 35; //only applies if same as height
	const MAX_WEIGHT = 20; //kgs
	const MAX_LENGTH = 105; //cms
	const MAX_GIRTH = 140; //cms
	const MIN_GIRTH = 16; //cms
 
        public function getRemoteData($url)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		  'Auth-Key: ' . $this->auth_key
		));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$contents = curl_exec ($ch);
		curl_close ($ch);
		return json_decode($contents,true);
	}
 
        public function getShippingCost($data)
	{
		$edeliver_url = "{$this->api}postage/parcel/domestic/calculate.json";
		$edeliver_url = $this->arrayToUrl($edeliver_url,$data);		
		$results = $this->getRemoteData($edeliver_url);
 
		if (isset($results['error']))
			throw new Exception($results['error']['errorMessage']);
 
		return $results['postage_result']['total_cost'];
	}
 
        public function arrayToUrl($url,$array)
	{
		$first = true;
		foreach ($array as $key => $value)
		{
			$url .= $first ? '?' : '&';
			$url .= "{$key}={$value}";
			$first = false; 	
		}	
		return $url;
	}
 
        public function getGirth($height,$width)
	{
		return ($width+$height)*2;
	}
}

to get the cost of an item to be shipped by regular post:

        $shipping = new Shipping();
        $data = array(
		'from_postcode' => 4511,
		'to_postcode' => 4030,
		'weight' => 10,
		'height' => 105,
		'width' => 10,
		'length' => 10,
		'service_code' => 'AUS_PARCEL_REGULAR'
	);
        try{
	         echo $shipping->getShippingCost($data);
        }
        catch (Exception $e)
        {
                 echo "oops: ".$e->getMessage();
        }

There are a lot more things you can do with the API, here is a link to the documentation.

I hope this has helped someone in figuring out how to get the API functioning.

Posted in PHP | 8 Comments

8 Responses to Australia Post API with PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 by damiandennis.com.
All Rights Reserved.