|
| 1 | +<?php |
| 2 | +/* |
| 3 | + A simple PHP class to query the Gandi API domain.price.list() |
| 4 | + with a list of domains based on a few popular extensions to |
| 5 | + obtain price & availability & phase in one API call. |
| 6 | +
|
| 7 | + Requirements: |
| 8 | + - A Gandi API key |
| 9 | + - XML_RPC2 from PHP PEAR |
| 10 | +
|
| 11 | + Usage example: |
| 12 | +
|
| 13 | + include 'DomainPrice.php'; |
| 14 | + apikey = 'your-Gandi-API-key'; |
| 15 | + $gapi = new DomainPrice($apikey); |
| 16 | + $gapi->doSearch("anydomainlabel"); // will search those extensions as defined in the class |
| 17 | + $results = $gapi->getCost(); // get the cost of the resulting available domains, returns cost & phase |
| 18 | + foreach ($results as $r) { |
| 19 | + echo $r['fqdn'] . "\t" . $r['available'] . " " . $r['cost'] . "\t" . $r['cost_vat'] . "\n"; |
| 20 | + } |
| 21 | +
|
| 22 | + Note: |
| 23 | + - Currency and price grid may be set with the setCurrency, setGrid function. |
| 24 | + - To include the VAT (under your reseller account) in the cost calculation enable it in the class: |
| 25 | + $cost_vat_calculation = true; |
| 26 | + $currency_has_decimals = true; (if your currency has decimal points) |
| 27 | + - Update $search_extensions to include other extensions. |
| 28 | +
|
| 29 | +*/ |
| 30 | + |
| 31 | +require_once 'XML/RPC2/Client.php'; |
| 32 | + |
| 33 | +class DomainPrice |
| 34 | +{ |
| 35 | + protected $api_key= ''; |
| 36 | + protected $domain_price_api; |
| 37 | + protected $search_time_seconds = 20; |
| 38 | + protected $search_extensions = array('.pizza','.tw','.taipei','.com','.com.tw','.blog','.online'); |
| 39 | + protected $search_results = array(); |
| 40 | + protected $domain_cost_list = array(); |
| 41 | + |
| 42 | + protected $grid = 'A'; |
| 43 | + protected $currency = 'USD'; |
| 44 | + protected $cost_vat_calculation = false; |
| 45 | + protected $currency_has_decimals = true; |
| 46 | + |
| 47 | + function getSearchResults() |
| 48 | + { |
| 49 | + return $this->search_results; |
| 50 | + } |
| 51 | + |
| 52 | + function getCost($includeUnavalable = false) |
| 53 | + { |
| 54 | + $this->domain_cost_list = array(); |
| 55 | + foreach ($this->search_results as $search_result) { |
| 56 | + if ($search_result["available"] === "available") { |
| 57 | + foreach ($search_result["prices"] as $price_item_prices) { |
| 58 | + $tax_rate = ($price_item_prices['taxes']['rate']); |
| 59 | + if ($search_result["current_phase"] === $price_item_prices["action"]["param"]["tld_phase"]) { |
| 60 | + foreach ($price_item_prices["unit_price"] as $unit_prices) { |
| 61 | + $price = $price_vat = $unit_prices['price']; |
| 62 | + if ($this->cost_vat_calculation == true) |
| 63 | + $price_vat += $price_vat * ($tax_rate/100); |
| 64 | + if ($this->currency_has_decimals == false) |
| 65 | + $price_vat = round($price_vat); |
| 66 | + |
| 67 | + $cost_element = array( |
| 68 | + 'fqdn' => $search_result["extension"], |
| 69 | + 'available' => $search_result["available"], |
| 70 | + 'phase' => $search_result["current_phase"], |
| 71 | + 'cost' => $price, |
| 72 | + 'cost_vat' => $this->cost_vat_calculation ? $price_vat : 0 |
| 73 | + ); |
| 74 | + array_push($this->domain_cost_list,$cost_element); |
| 75 | + |
| 76 | + if ($unit_prices['special_op'] == true) |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + else { |
| 83 | + if ($includeUnavalable) { |
| 84 | + $cost_element = array( |
| 85 | + 'fqdn' => $search_result["extension"], |
| 86 | + 'available' => $search_result["available"], |
| 87 | + 'phase' => $search_result["current_phase"], |
| 88 | + ); |
| 89 | + array_push($this->domain_cost_list,$cost_element); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + return $this->domain_cost_list; |
| 94 | + } |
| 95 | + |
| 96 | + function setCurrency($currency) |
| 97 | + { |
| 98 | + $this->currency = $currency; |
| 99 | + } |
| 100 | + |
| 101 | + function setGrid($grid) |
| 102 | + { |
| 103 | + $this->grid = $grid; |
| 104 | + } |
| 105 | + |
| 106 | + function doSearch($search_label) |
| 107 | + { |
| 108 | + $fqdn_list = array(); |
| 109 | + |
| 110 | + foreach ($this->search_extensions as $extension) { |
| 111 | + array_push($fqdn_list,$search_label . $extension); |
| 112 | + } |
| 113 | + |
| 114 | + $search_results = array(); |
| 115 | + |
| 116 | + for ($i = 0;$i<$this->search_time_seconds;$i++) { |
| 117 | + |
| 118 | + $price_args = array( |
| 119 | + 'tlds' => $fqdn_list, |
| 120 | + 'grid' => $this->grid, |
| 121 | + 'currency' => $this->currency, |
| 122 | + 'action' => 'create', |
| 123 | + ); |
| 124 | + |
| 125 | + $price_listing = $this->domain_price_api->list( $this->api_key, $price_args); |
| 126 | + |
| 127 | + foreach ($price_listing as $price_item) { |
| 128 | + if ($price_item["available"] !== "pending") { |
| 129 | + array_push($this->search_results,$price_item); |
| 130 | + foreach (array_keys($fqdn_list, $price_item['extension'], true) as $key) { |
| 131 | + unset($fqdn_list[$key]); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if (sizeof($fqdn_list) == 0) |
| 137 | + break; |
| 138 | + |
| 139 | + sleep(1); |
| 140 | + } |
| 141 | + |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + function __construct($api_key) |
| 146 | + { |
| 147 | + $this->api_key = $api_key; |
| 148 | + $this->domain_price_api = XML_RPC2_Client::create ( |
| 149 | + 'https://rpc.gandi.net/xmlrpc/', |
| 150 | + array('prefix' => 'domain.price.','sslverify' => False) |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +?> |
0 commit comments