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
- PHP Curl Extension must be installed.
- An API Key register here
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.
Fantastic.
THanks Damian!!
Hey guys, Sorry about the really late reply, as hoogs found above that the
&
should have been just &. I have updated the code above and should work now. CheersHi Damian, thanks for this great article.
I have the exact same issue as Dan. Did you guys fix the issue?
Many thanks
Luc
I was having the same issue. It seems auspost doesn’t like a correctly formatted url using ‘&’ but works if you lazily use ‘&’ only. ‘from_postcode’ works because it comes before the first ‘&’ but ‘to_postcode’ (and everything after) doesn’t register.
Replace this line in the shipping class
$url .= $first ? '?' : '&';
…with this
$url .= $first ? '?' : '&';
…all sorted.
Brilliant code btw.
I should have realised it wasn’t going to display the amp; after the & in my post. Same thing clearly happened to Dan.
Here’s a strategically placed space in the line to change so you can see the issue:
$url .= $first ? '?' : '& amp;';
with amp; removed, becomes…
$url .= $first ? '?' : '&';
Hi Damian, I have also tried your code with my API key and receive the same error?
oops: Please enter To postcode.
Great work. I am having a problem, I have used your exact example with a new registered api key. All I get is an error “oops: Please enter To postcode.” The edeliver_url curl is sending is “https://auspost.com.au/api/postage/parcel/domestic/calculate.json?from_postcode=4511&to_postcode=2000&weight=2&height=10&width=10&length=10&service_code=AUS_PARCEL_REGULAR”.
Not sure what else to check..
Thanks,Dan.
Dan,
That should work. maybe send through a link to your example or paste send me some code through an email to damiandennis at gmail dot com.