Australia Post API and PHP

This is a tutorial on setting up the Australia Post API with PHP, The documentation was lengthy and explained it well but did not provide any examples and is why I have written this blog post. Once you know how it is simple.

Prerequisites

  • PHP Curl Extension must be installed.
  • An API Key, you can get one here

I have chosen to do this in object orientated style as I avoid proceedural programming where ever possible.

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

The First is the API url and the second is the Authentication key you got after registering with Australia Post.

The following method is for interaction with the API and is why curl is required.

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 data from the API and we convert the JSON to an array.

Structure

I have added some constants that the values that the API restricts on and some methods for grabbing the information we need from Australia Post.

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;
	}
}

Example

Here is an example of how to get the cost of shipping for a parcel 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();
        }

The following link has all the technical information for you to make good use of this API. documentation.

This should be enough for most people to get started.

Leave a Reply

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