Domain API Loop in PHP

How can I perform the following with this API?

Form is setup in PHP for registered user to enter domain name
On submit…

Check if domain is available >

  • If NO > output to XML > run the loop again in 5 minutes
  • If YES > output to XML > go ahead and purchase & register the domain > end loop

Thank you!

Sample API code

    <?php /**  * Set the API URL  */ $sApiUrl = "https://www.apiurl.com/";
   
   
    /**  * Set POST Parameters  */ $aParams = Array(
                'uid'     => "--USERNAME--",    // Username
                'pw'      => "--APIKEY--",      // API Key
                'command' => "querydomain",     // Command to Rum
                'sld'     => "sampledomain",    // Main part of the Domain
                'tld'     => "com");         // Domain Extension
   
   
    /**  * Run the cURL command  */
    $oCurl = curl_init(); curl_setopt($oCurl, CURLOPT_URL, $sApiUrl);
    curl_setopt($oCurl, CURLOPT_POST, 1);
    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $aParams);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
    $sResponse = curl_exec($oCurl); curl_close($oCurl);
   
   
    /**  * Turn results into Simple XML Object  */
    $oOutput = new SimpleXmlElement($sResponse);
   
   
    /**  * Debugging output, so we can see what we just got  */
    print_r($oOutput);
   
    ?>

API Commands

check Checks whether a domain name is available for registration.
purchase Registers a new domain name.
transfer Requests a domain transfer to registrar.
extend Extends (renews) domain registration.
changepassword Changes the domain password.
sendpassword Send domain password to domain owner.
contacts Changes domains contacts.
querydomain Returns detailed information about the domain.
sendepp Sends gTLD epp key to domain owner.
refillaccount Refills your reseller account with funds.
changedomainns Changes domains name servers.
sendtransferemail Sends transfer authorisation request to domain owner.
addchildns Add child name server to domain.
updatechildns Update details for child name server.
removechildns Remove child name server from domain.
pushdomain Push domain to another registrar reseller.
getdnsrecords Get domain name server records for a domain.
enablednsdomain Create DNS domain service for domain.
adddnsrecord Add new DNS record to domain.
updatednsrecord Update DNS record for a domain.
removednsrecord Remove DNS record from a domain.
updatednssoa Update DNS SOA record for a domain.
disablednsdomain Destroy DNS domain service for a domain.
getmailrecords Get mail forwarding records for a domain.
addmailforwarder Add a mail forwarder to a domain.
removemailforwarder Remove a mail forwarder from a domain.
updatemailforwarder Update the details of a Mail Forwarder for a domain.
geturlrecords Get URL Forwarding records for a domain.
addurlforwarder Add a URL Forwarding record to a domain.
updateurlforwarder Update a URL Forwarding record for a domain.
removeurlforwarder Remove a URL Forwarding record for a domain.
getreglock Get registry lock details for a domain.
setreglock Set a registry lock for a domain.
updatecontact Update an address book entry.
getapiinfo Get information about the Domain System API.
transferout Push a domain name to another registrar, given their tag.

Welcome to SitePoint! :slight_smile:

Why would you need to run it again if the domain is not available ?

Thanks AnthonySterling! :slight_smile:

Ok so the domain is deleted and about to be purged from the registry…
Checks if available (if so, purchase & register), if it’s unavailable, check it again and again and again, until it IS available :slight_smile: (maybe even with a max time limit)

I hope that makes it a little clearer.

Ah, I see.

Store a list of domains in a database (which you probably have), then have a CRON job ask for a list of unregistered domains every 5 minutes.

SELECT domain FROM queue WHERE is_registered = false LIMIT 5

If you manage to register the domain, set the is_registered flag to true. The next time the CRON job runs, this domain will not be in that list.

The API documentation for the [URL=“https://domains.aussiehq.com.au/api/help?q=cmd:check”]check command let you use a comma-delimited list of domains, so this should save you from repeated calls.

How does that sound? :slight_smile:

Sound perfect! though it’ll be a lot of work for me through customising WHMCS… is there an alternate way through a PHP loop? Sorry.

Thank you for the help!

Not easily, PHP will time-out which is why we use a CRON job to repeatedly invoke a small PHP script to do the job.

Not a problem, thank you.

Now, setting up the cron, db and the script… forgive my lack of programming knowledge, but how could I go about setting it all up (in summary or even with examples) - I’m using cPanel so setting up the cron shouldn’t be difficult… just haven’t used it before.

Last thing is, can I reduce the timeout to seconds rather than minutes?

Well, your PHP script to do the work would go along the lines of…


<?php
$res = mysql_query('SELECT domain FROM table WHERE is_registered = false LIMIT 5;');

if(0 === mysql_num_rows($res)){
  exit;
}

$registered = array();

while($row = mysql_fetch_assoc($res)){
  if(DOMAIN_AVAILABLE){
    if(DOMAIN_SUCCESSFULLY_REGISTERED){
      array_push($registered, $row['domain'])
    }
  }
}

if(0 < count($registered)){
  mysql_query(sprintf(
    "UPDATE table SET is_registered = true WHERE domain IN ('%s');",
    implode(
      "','",
      array_map('mysql_real_escape_string', $registered)
    )
  ));
}
?>

Then, just set up a CRON job to invoke the script every x minute(s). Every minute is the shortest interval you can use.

You could even get fancy and store the date the domain is due to expire and the script would automatically renew it for you.

WOW! Thank you Anthony!!
Sounds so much better than a million loops if there is multiple domains!!

So that will be the script for the cron… I now need to create a script to check and purchase through the API?

Sort of confused as to how everything connects and is coded :frowning:

Creating the table with rows domain and is_registered, is not a problem. It’s the API / CRON integration that I’m confused on. Once the cron is run, there has to be part of the script which connects to the API, does the check and returns available or not? If it’s available… register…?

No problem. :slight_smile:

You connect to the API, check and register the domians in this part of the above script…


  if(DOMAIN_AVAILABLE){
    if(DOMAIN_SUCCESSFULLY_REGISTERED){
      array_push($registered, $row['domain'])
    }
  }

Ok for the top area… let me give this a quick try…


    /**  * Set the API URL  */ $sApiUrl = "https://www.apiurl.com/";
    /**  * Set POST Parameters  */ $aParams = Array(

                'uid'     => "--USERNAME--",    // Username
                'pw'      => "--APIKEY--",      // API Key
                'command' => "check",     // Command to Run
                'sld'     => "sampledomain",    // Main part of the Domain
                'tld'     => "com");         // Domain Extension

  if(DOMAIN_AVAILABLE){
                'command' => "purchase",     // Command to Run
    if(DOMAIN_SUCCESSFULLY_REGISTERED){
      array_push($registered, $row['domain'])
    }
  }

Kind of understanding, just not sure if those variables you used ‘DOMAIN_AVAILABLE’ and DOMAIN_SUCCESSFULLY_REGISTERED were actual API returns or a generic placement for me to replace with code?

Almost there, but I’m definitely all ears! :slight_smile:
Thanks!

Ran the original example code and it outputs as follows;

SimpleXMLElement Object ( [results] => SimpleXMLElement Object ( ) [errors] => SimpleXMLElement Object ( [error] => SimpleXMLElement Object ( [domain] => SimpleXMLElement Object ( [name] => google.com [status] => SimpleXMLElement Object (

 =&gt; 211 [text] =&gt; Unavailable [description] =&gt; The domain name you queried has been registered through another registrar. ) ) ) ) [errorcount] =&gt; 1 [exectime] =&gt; 0.357 second(s) [enviroment] =&gt; live [version] =&gt; 2.3.9 beta )

May help with the 'check' output code?

Sorry, here it is, outputted with correct XML formatting;

SimpleXMLElement Object
(
    [results] => SimpleXMLElement Object
        (
        )

    [errors] => SimpleXMLElement Object
        (
            [error] => SimpleXMLElement Object
                (
                    [domain] => SimpleXMLElement Object
                        (
                            [name] => google.com
                            [status] => SimpleXMLElement Object
                                (
                                    

=> 211
[text] => Unavailable
[description] => The domain name you queried has been registered through another registrar.
)

                    )

            )

    )

[errorcount] =&gt; 1
[exectime] =&gt; 0.399 second(s)
[enviroment] =&gt; live
[version] =&gt; 2.3.9 beta

)

Hey. :slight_smile:

You just need to check the return code (211 in this case), however the API documentation states the return code may appear in 2 different location within the response dependent on where it was raised.

ie. API or Registrar.

The location would also change if you sent over a list of domains, in this case you would have to iterate through the returned results are check them individually.

So would check if the domain was available (return code 210 if so), then issue a command to register it and see if that too was successful (return code 200 or 220, the docs are ambiguous).

Thanks Anthony :slight_smile:

No idea how I’m going to do this… but well worth a shot! Trial and error? lol

Thank you again - btw, i’ve voted for you for several awards on sitepoint! I think you’re level of support is oustanding!! above and beyond expectations.