Australian Government ABN Lookup – Webservice – PHP Example

The PHP example code supplied by http://abr.business.gov.au/Webservices.aspx relied on the nuSoap class. I rewrote it to fit our companies needs, based on what I’ve learned from developing the PPSR Soap class I posted earlier. I hope someone finds this useful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
include(dirname(__FILE__)."/config.inc.php"); // $abn_guid is set here.
 
/**
 * @author Justin Swan - 16 August 2012
 * extends php soap client to utilize the Australian Government ABN Lookup web service 
 * requires php 5 or greater with lib-xml enabled/compiled in Apache, see the PHP manual for further requirements info
 * 
 * @link    http://www.php.net/manual/en/book.soap.php
 * @link    http://abr.business.gov.au/Webservices.aspx
 * 
 * @param string $guid - get a guid id by registering @ http://abr.business.gov.au/Webservices.aspx
 * 
 */
 
class abnlookup extends SoapClient{
 
    private $guid = ""; 
 
    public function __construct($guid)
    {
        $this->guid = $guid;
        $params = array(
            'soap_version' => SOAP_1_1,
            'exceptions' => true,
            'trace' => 1,
            'cache_wsdl' => WSDL_CACHE_NONE
        ); 
 
        parent::__construct('http://abr.business.gov.au/abrxmlsearch/ABRXMLSearch.asmx?WSDL', $params);
    }
 
    public function searchByAbn($abn, $historical = 'N'){
        $params = new stdClass();
        $params->searchString                = $abn;
        $params->includeHistoricalDetails    = $historical;
        $params->authenticationGuid            = $this->guid;
        return $this->ABRSearchByABN($params);
    }
 
    public function searchByName($company_name){
        $params = new stdClass();
        $params->externalNameSearch = $company_name;
        $params->authenticationGuid            = $this->guid;
        return $this->ABRSearchByName($params);
    }
 }
 
$abn_search_string = "11111111111"; // you can assign your post/get var or abn string here
$name_search_string = "some company name"; // replace with the company name you are trying to search
 
try{
    $abnlookup = new abnlookup($abn_guid);
    try{
        $result = $abnlookup->searchByAbn($abn_search_string); 
        // $result = $abnlookup->searchByName($name_search_string); 
 
        // display all results
        echo "<pre>";
        print_r($result);
        echo "</pre>";
 
        // also display by variables using object notation.
        echo "<pre>";
        $result->ABRPayloadSearchResults->response;
        echo "</pre>";
 
    } catch    (Exception $e){
        throw $e;
    }
 
} catch(Exception $e){
    echo $e->getMessage();
}

Now on GitHub: https://github.com/Kwozzie/australian-government-abn-lookup

18 thoughts on “Australian Government ABN Lookup – Webservice – PHP Example”

  1. Hi,

    I am very new to this, I used your sample and worked! much better than the official sample. Great sample!!

    But my question now is how do I extract the data for my own use?

  2. Hi Daniel,

    The two ‘output’ examples of echo and print_r should be enough to determine what you need. I’m not sure how I can answer your question better without further information?

  3. Thanks for the reply Justin.

    I am actually asking that, since I got the out out result. But how do I like capture the content. EG: I would want to create a variable $company_name and then have the company name from the result to be stored into it.

    Also, just wonder, is it possible to search for a company’s ABN with the company name instead?

    Thanks!

  4. You mean something like this?


    try{
    $abnlookup = new abnlookup($abn_guid);
    try{
    $result = $abnlookup->searchByAbn($abn_search_string);

    $response = $result->ABRPayloadSearchResults->response;
    if(isset($response->exception)){
    echo "Error: ".$response->exception->exceptionDescription;

    } else {

    $company_name = $response->businessEntity->mainName->organisationName;

    }

    } catch (Exception $e){
    throw $e;
    }

    } catch(Exception $e){
    echo $e->getMessage();
    }

    If you look at http://abr.business.gov.au/abrxmlsearch/ABRXMLSearch.asmx?WSDL in your browser, you can see other methods/calls that are supported. ABRSearchByName is the one you are after, I believe.

    I’ve added a method to the class example to help.
    Cheers, Justin

  5. Justin,

    That’s just what I was looking for!! Thanks so much!!!

    Have you tried the reverse? Search the company name & get the ABN in return??

  6. No worries. No, I haven’t tried it but I would imagine your responses could be a bit ‘ambiguous’ for want of a better word, given that there may be two or three company names that are all similar. I’m not sure whether it works off an exact or near match.

  7. Hi Justin,

    I found your code very useful thanks for your help.

    regards,
    Mario

  8. Hi, could you help me as I am looking for a soap which returns me Business location for a given abn number or organization name

  9. Please any one tell me how to use this and i got guid and i want to know how to access the ABN webservices it always reply

    [exceptionDescription] => The GUID entered is not recognised as a Registered Party
    [exceptionCode] => WEBSERVICES

    whats wrong with my guid?

  10. Hi,
    I tried this code to search ABN by name, but I got fatal error such “PHP Fatal error: Call to undefined method stdClass::externalNameSearch()”

    Please help.

  11. Hi Raja,

    Firstly you’re trying to call a non-static method statically, secondly, the error messages says undefined ‘stdClass’ so the method isn’t being called through an instantiated object. Sorry, I can’t help more without seeing your code.

  12. guid = $guid;
    $params = array(
    ‘soap_version’ => SOAP_1_1,
    ‘exceptions’ => true,
    ‘trace’ => 1,
    ‘cache_wsdl’ => WSDL_CACHE_NONE
    );

    parent::__construct(‘http://abr.business.gov.au/abrxmlsearch/ABRXMLSearch.asmx?WSDL’, $params);
    }

    public function searchByAbn($abn, $historical = ‘N’){
    $params = new stdClass();
    $params->searchString = $abn;
    $params->includeHistoricalDetails = $historical;
    $params->authenticationGuid = $this->guid;
    return $this->ABRSearchByABN($params);
    }

    public function searchByName($company_name){
    $params = new stdClass();
    $params->externalNameSearch($company_name);
    $params->authenticationGuid = $this->guid;
    return $this->ABRSearchByName($params);
    }
    }
    $company_name = “A. & G. PTY LTD”;
    $abn_search_string = “74599608295”; // you can assign your post/get var or abn string here

    $abn_guid =”***********************”;
    try{
    $abnlookup = new abnlookup($abn_guid);
    try{
    $result = $abnlookup->searchByName($company_name);

    // display all results
    echo “”;
    print_r($result);
    echo “”;

    // also display by variables using object notation.
    echo “”;
    $result->ABRPayloadSearchResults->response;
    echo “”;

    } catch (Exception $e){
    throw $e;
    }

    } catch(Exception $e){
    echo $e->getMessage();
    }

    config.inc.php is missing. what should with contain?

  13. Even though it’s shows fatal error such “PHP Fatal error: Call to undefined method stdClass::externalNameSearch()”.

    The code is given below:
    guid = $guid;
    $params = array(
    ‘soap_version’ => SOAP_1_1,
    ‘exceptions’ => true,
    ‘trace’ => 1,
    ‘cache_wsdl’ => WSDL_CACHE_NONE
    );

    parent::__construct(‘http://abr.business.gov.au/abrxmlsearch/ABRXMLSearch.asmx?WSDL’, $params);
    }

    public function searchByAbn($abn, $historical = ‘N’){
    $params = new stdClass();
    $params->searchString = $abn;
    $params->includeHistoricalDetails = $historical;
    $params->authenticationGuid = $this->guid;
    return $this->ABRSearchByABN($params);
    }

    public function searchByName($company_name){
    $params = new stdClass();
    $params->externalNameSearch($company_name);
    $params->authenticationGuid = $this->guid;
    return $this->ABRSearchByName($params);
    }
    }
    $company_name = “A. & G. PTY LTD”;
    $abn_search_string = “74599608295”; // you can assign your post/get var or abn string here

    $abn_guid =”******************************”;
    try{
    $abnlookup = new abnlookup($guid);
    try{
    $result = $abnlookup->searchByName($company_name);

    // display all results
    echo “”;
    print_r($result);
    echo “”;

    // also display by variables using object notation.
    echo “”;
    $result->ABRPayloadSearchResults->response;
    echo “”;

    } catch (Exception $e){
    throw $e;
    }

    } catch(Exception $e){
    echo $e->getMessage();
    }

  14. My apologies Raja, there was a mistake in that code (now corrected, but untested) which the error message was quite obvious about. Please try again using the example. I was trying to assign the $company_name to the generic class value externalNameSearch by passing it as a method param (never going to work in this case).

    Cheers, Justin

  15. Good morning Justin,
    Justin this is fantastic, I’m relatively new to php and having a working example like this, and making it easy to follow is a gem.

    I did get stuck for a while on the same question Daniel had a number of years back. I could understand what was going, but the syntax eluded me. With your update in this thread was a ‘doh’ moment for me.

    Really cool.

    PS, I am about to start working on the PPSR integration myself, so your example is going to be looked at with much enthusiasm.

    Kind Regards

Leave a Reply

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