Skip to content

Instantly share code, notes, and snippets.

@arian
Created August 22, 2010 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arian/544018 to your computer and use it in GitHub Desktop.
Save arian/544018 to your computer and use it in GitHub Desktop.
A class that fetches data from http://whomsy.com and checks if a domain is available
<?php
// A class that fetches data from http://whomsy.com and checks if a domain is available
class Whomsy {
public $server = 'http://whomsy.com/api/%s';
public $timeout = 5;
public static $AVAILABILITIES = array(
'be' => 'Status: FREE',
'biz' => 'Not found',
'com' => 'No match for',
'de' => 'not found in database',
'eu' => 'Status: AVAILABLE',
'in' => 'NOT FOUND',
'info' => 'NOT FOUND',
'it' => 'Status: AVAILABLE',
'name' => 'No match.',
'net' => 'No match for',
'nl' => 'is free',
'nu' => 'NO MATCH for domain',
'org' => 'NOT FOUND',
'tv' => 'No match for',
'us' => 'Not found:',
);
protected $domain;
protected $info;
public function __construct($domain=null){
$this->setDomain($domain);
}
public function setDomain($domain){
if (substr($domain, 0, 7) != 'http://') $domain = 'http://'.$domain;
$domain = parse_url($domain, PHP_URL_HOST);
if (substr($domain, 0, 4) == 'www.') $domain = substr($domain, 4);
$this->domain = $domain;
return $this;
}
public function getDomain(){
return $this->domain;
}
public function defineAvailability($tld, $search){
self::$AVAILABILITIES[$tld] = $search;
}
public function getInfo(){
if ($this->info !== null) return $this->info;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, sprintf($this->server, $this->domain));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data, true);
$this->info = ($data['type'] == 'success') ? $data : false;
return $this->info;
}
public function isAvailable(){
$tld = substr($this->domain, strpos($this->domain, '.') + 1);
if (isset(self::$AVAILABILITIES[$tld])){
$search = self::$AVAILABILITIES[$tld];
$info = $this->getInfo();
if (!$info) return false;
return stripos($info['message'], $search) !== false;
}
return false;
}
}
<?php
echo '<pre>';
function test($name, $result){
echo '<div style="margin: 30px; background: '.($result ? 'green' : 'red').'">'.$name.'<br />'.$result.'</div>';
}
include 'Whomsy.php';
$whomsy = new Whomsy('http://www.aryweb.nl');
test('Domain', $whomsy->getDomain() == 'aryweb.nl');
$whomsy->setDomain('arydfgsdtrgweb.com');
test('info', var_export($whomsy->getInfo(), true));
test('availability', $whomsy->isAvailable() == true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment