Wednesday, 5 June 2013

API integration in php - Domain Check Availability and Suggestions Using Reseller club API

Checks the availability of the specified domain name(s).

Parameters


NameData TypeRequired / OptionalDescription
auth-useridIntegerRequired
auth-userid: Your Live Account Reseller Id
(See the instructions below for finding your
Reseller Id).
api-key (or) auth-passwordStringRequiredapi-key: An alphanumeric code that can be 
used to authenticate your API calls (See the 
instructions below for finding your API Key),
(OR)
auth-password: The password you use to 
login to your Live Account's Reseller Control 
Panel.
domain-nameArray of StringsRequiredDomain name(s) that you need to check the
availability for.
tldsArray of StringsRequiredTLDs for which the domain name availability
needs to be checked.
suggest-alternativeBooleanOptionalPass true if domain name suggestions are
required. Default value is false.

Locating your Reseller IdTop

  1. Login to your Reseller Control Panel. 1
  2. Click the  icon at the top right corner of the page and then click Manage Profile.
  3. In the Reseller Profile Details view that is now displayed, the first field indicates your Reseller Id.

Locating your API KeyTop

  1. Login to your Reseller Control Panel. 2
  2. In the Menu, point to Settings and then click API.
  3. Your unique API Key will be listed under the API Key section.
    You may generate a fresh API Key by clicking the  icon and then confirming Key regeneration.

Example Test URL RequestTop

https://test.httpapi.com/api/domains/available.json?auth-userid=0&api-key=key&domain-name=domain1&domain-name=domain2&tlds=com&tlds=net

ResponseTop

Returns a hash map containing domain name availability status for the requested TLDs:

Available - domain name available for registration

Regthroughus - domain name currently registered through the Registrar whose connection is being used to check the availability of the domain name


Regthroughothers - domain name currently registered through a Registrar other than the one whose connection is being used to check the availability of the domain name. If you wish to manage such a domain name through your Reseller / Registrar Account, you may pass a Domain Transfer API call. 1


Unknown - returned, if for some reason, the Registry connections are not available. You should ideally re-check the domain name availability after some time.

Example: File Name : checkdomain.php

<?php

$api_user_id="Your user id";
$api_key="key";


function domainCallAPI($method, $url, $data = false) {
    $curl = curl_init();

    switch ($method) {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    // Optional Authentication: - Need not touch this
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "username:password");
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($curl);
}
?>

<?php
//Example Call to see available domains and suggestions
$url = '';
$tldarray[] = "";
$domainname="";
if (isset($_POST['check']) && isset($_POST['domain']) && $_POST['domain'] != "" && isset($_POST['tld']) && $_POST['tld'] != "") {

    $domainname = $_POST['domain'];
    $tld = "";
    $i = 0;
    foreach ($_POST['tld'] as $arrayitem) {
        $tld.='&tlds=' . $arrayitem;
        $tldarray[$i] = $arrayitem;
        $i++;
    }

// $api_user_id and  $api_key are initialized in config file or directly in in this page
    $url = 'https://test.httpapi.com/api/domains/available.json?auth-userid=' . $api_user_id . '&api-key=' . $api_key . '&domain-name=' . $domainname . $tld . '&suggest-alternative=true';
    $data = "";
    $data = domainCallAPI('GET', $url, $data);
    $datajson = json_decode($data, TRUE);
    $fulldomainname = "";
    //  $flag=0;
    foreach ($tldarray as $arrayitem) {

        $fulldomainname = $domainname . "." . $arrayitem;
        echo '<br/>' . $fulldomainname;
        if ($datajson[$fulldomainname]["status"] == "available") {
            echo '    Available';
            // $flag=1;
        } else {
            echo '    Not Available <br/>';
        }
    }
    // Domain Suggestions
    $stack = array("");
    $i = 0;
    foreach ($datajson[$domainname] as $sugdomain => $sugtld) {
        // echo $sugdomain.'.'.$tld;

        $stack[$i] = $sugdomain;
        $i++;
    }
    echo '<br/>Sugg names : <br/>';
    $i = 0;
    foreach ($stack as $value) {
        foreach ($datajson[$domainname][$value] as $sugdomain => $sugtld) {
            if ($datajson[$domainname][$value][$sugdomain] == "available") {
                echo $value . '.' . $sugdomain;
                echo '<br/>';
                $i++;
                if ($i == 7) {

                    break;
                }
            }
        }

        if ($i == 7) {

            break;
        }
    }
    // domain suggestions end
}
 elseif (isset($_POST['check'])){
 
    if(!isset($_POST['tld'])){
      echo '<br/> Tick Your Domain';
 
    }
     if( !isset($_POST['domain']) || $_POST['domain'] =="" ){
        echo '<br/> Type Domain Name';  
    }else{
        $domainname=$_POST['domain'];
    }

}

?>

<form method="post" action="checkdomain.php">
    <input type="text" name="domain" value="<?php echo $domainname; ?>"/>

    <input type="checkbox" name="tld[]" value="com"  <?php  if(!isset($_POST['check'])){  ?> checked="checked" <?php } elseif (in_array("com", $tldarray)) {  ?> checked="checked" <?php  } ?> />com
    <input type="checkbox" name="tld[]" value="net" <?php if (in_array("net", $tldarray)) {  ?> checked="checked" <?php  } ?> />net
    <input type="checkbox" name="tld[]" value="in" <?php if (in_array("in", $tldarray)) {  ?> checked="checked" <?php  } ?> />in
    <input type="checkbox" name="tld[]" value="biz" <?php if (in_array("biz", $tldarray)) {  ?> checked="checked" <?php  } ?> />biz
    <input type="checkbox" name="tld[]" value="org" <?php if (in_array("org", $tldarray)) {  ?> checked="checked" <?php  } ?> />org
    <input type="checkbox" name="tld[]" value="asia" <?php if (in_array("asia", $tldarray)) {  ?> checked="checked" <?php  } ?> />asia


    <input type="submit" name="check" value="Check"/>
</form> 

I hope all goes well! Look forward to seeing more post from me in the future.

18 comments:

  1. how I making test from my local machine

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi please help me I want to develop an example to Check Availability - IDN using using club reseller API with PHP I do I do not understand "idnLanguageCode" how it works for Example many languages more than one language code as the Chinese have: chi, zh, zh-co valid code for each different IDN

      Delete
  3. Thank you!!!! Been looking for exactly this script for the past week, huge time saver, I really appreciate it.

    ReplyDelete
  4. pls where do i past the API code in my website

    ReplyDelete
  5. It is a good blog..You have provided a very useful information about domain check.

    value calculator

    ReplyDelete
  6. I have api key and id andd paste that but it is not working

    ReplyDelete
  7. pretty nice and good information you have shared thanks for this all keep it up

    ReplyDelete
  8. I'm new in API can you please tell me where I can paste this code to active in my website...

    ReplyDelete
  9. Great example code. Works. I added a few lines of code to display any errors I got from access permission errors from ResellerClub.

    $fulldomainname = "";
    //Added to display the raw json string returned to display errors.
    if ($datajson["status"] == "ERROR") {
    echo $data;
    };
    // $flag=0;

    Add this if your getting any php errors that get written to your error.log file. If the function call gets a permission error from ResellerClub api then the json will contain status of ERROR and you can check for that and resolve with ResellerClub. This error creates a cascading effect lower in the code because it does not return any domain name array to fill up the for loops lower in the code and they throw errors. Hope this helps folks that are getting errors and you are wondering if it is the codes fault. Look to ResellerClub api docs for more information on getting permission to use the api.

    ReplyDelete
    Replies
    1. Also a note.... if you get an error from reseller api then it will report that the domain is not available because that is the only thing the code provides for in the loop if statement and if the status of the domain is not provided for in the json string then it falls through and get tagged as not available.

      Delete
  10. Thanks foe sharing this information. It is Quite Simple to Integrate Bulk SMS API PHP, all you need to do . As it is instant delivery users can get the respective codes from API panel within a fraction of seconds.

    ReplyDelete

Simple CRUD in Laravel Framework

Creating, reading, updating, and deleting resources is used in pretty much every application. Laravel helps make the process easy using reso...