Created
November 18, 2012 14:30
-
-
Save sumardi/4105574 to your computer and use it in GitHub Desktop.
MyDNSToolBox API PHP class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| define('TOKEN', 'INSERT_YOUR_TOKEN_HERE'); | |
| require_once 'mydnstoolbox.php'; | |
| $t = new MyDNSToolbox(TOKEN, 'json'); | |
| // var_dump($t->createZone('domain.com', 'me@sumardi.net')); | |
| // var_dump($t->deleteZone('domain.com')); | |
| // var_dump($t->createRecord('domain.com', 'testapi', 'A', '202.188.0.133')); | |
| // var_dump($t->deleteRecord('domain.com', 'testapi', 'A', '202.188.0.133')); | |
| // var_dump($t->showZones()); | |
| // var_dump($t->showRecords('domain.com')); | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class MyDNSToolbox | |
| { | |
| /** | |
| * @var string API endpoint | |
| */ | |
| private $host = 'http://mydns.my/api/v1'; | |
| /** | |
| * @var string API token | |
| */ | |
| private $token = null; | |
| /** | |
| * @var string Output format | |
| */ | |
| private $format = 'json'; | |
| /** | |
| * @var integer Set timeout default. | |
| */ | |
| private $timeout = 30; | |
| /** | |
| * @var boolean Verify SSL Cert | |
| */ | |
| private $ssl_verifypeer = FALSE; | |
| /** | |
| * @var integer Connection timeout | |
| */ | |
| private $connecttimeout = 30; | |
| /** | |
| * @var string User-agent | |
| */ | |
| private $useragent = 'MyDNSToolbox/Client v1.3.4'; | |
| /** | |
| * Constructor method | |
| * | |
| * @access public | |
| * @param string token Token | |
| * @param string format Output format, 'json' or 'xml' | |
| * @return void | |
| */ | |
| public function __construct($token, $format = 'json') { | |
| $this->token = $token; | |
| $this->format = $format; | |
| } | |
| /** | |
| * Returns Authentication test URL | |
| * | |
| * @access public | |
| * @param string format Output format. 'json' or 'xml' | |
| * @return string test url | |
| */ | |
| public function authTestURL($format = 'json') { | |
| switch($format) { | |
| case 'json': | |
| return $this->host . '/zones/test.json'; | |
| break; | |
| case 'xml': | |
| return $this->host . '/zones/test.xml'; | |
| default: | |
| break; | |
| } | |
| } | |
| /** | |
| * Returns the zone URL | |
| * | |
| * @access public | |
| * @param string format Output format. 'json' or 'xml' | |
| * @return string zone url | |
| */ | |
| public function zoneURL($format) { | |
| switch($format) { | |
| case 'json': | |
| return $this->host . '/zones.json'; | |
| break; | |
| case 'xml': | |
| return $this->host . '/zones.xml'; | |
| default: | |
| break; | |
| } | |
| } | |
| /** | |
| * Returns the create record URL | |
| * | |
| * @access public | |
| * @param string format Output format. 'json' or 'xml' | |
| * @return string create record url | |
| */ | |
| public function createRecordURL($format) { | |
| switch($format) { | |
| case 'json': | |
| return $this->host . '/zones/create_record.json'; | |
| break; | |
| case 'xml': | |
| return $this->host . '/zones/create_record.xml'; | |
| default: | |
| break; | |
| } | |
| } | |
| /** | |
| * Returns the create record URL | |
| * | |
| * @access public | |
| * @param string format Output format. 'json' or 'xml' | |
| * @return string create record url | |
| */ | |
| public function deleteRecordURL($format) { | |
| switch($format) { | |
| case 'json': | |
| return $this->host . '/zones/delete_record.json'; | |
| break; | |
| case 'xml': | |
| return $this->host . '/zones/delete_record.xml'; | |
| default: | |
| break; | |
| } | |
| } | |
| /** | |
| * Returns the show record URL | |
| * | |
| * @access public | |
| * @param string format Output format. 'json' or 'xml' | |
| * @return string create record url | |
| */ | |
| public function showRecordURL($format) { | |
| switch($format) { | |
| case 'json': | |
| return $this->host . '/zones/show.json'; | |
| break; | |
| case 'xml': | |
| return $this->host . '/zones/show.xml'; | |
| default: | |
| break; | |
| } | |
| } | |
| /** | |
| * Test authentication | |
| * | |
| * @access public | |
| * @return string API result | |
| */ | |
| public function testAuth() { | |
| return $this->http($this->authTestURL($this->format), 'GET', null, $this->to_header()); | |
| } | |
| /** | |
| * Create zone and user | |
| * | |
| * @access public | |
| * @param string zone the zone | |
| * @param string user user email address that responsible for the zone | |
| * @return string API response | |
| */ | |
| public function createZone($zone, $user) { | |
| $data = array('zone' => $zone, 'user' => $user); | |
| return $this->http($this->zoneURL($this->format), 'POST', http_build_query($data), $this->to_header()); | |
| } | |
| /** | |
| * Show zones | |
| * | |
| * @access public | |
| * @return string API response | |
| */ | |
| public function showZones() { | |
| return $this->http($this->zoneURL($this->format), 'GET', null, $this->to_header()); | |
| } | |
| /** | |
| * Show records | |
| * | |
| * @access public | |
| * @param string domain The domain | |
| * @return string API response | |
| */ | |
| public function showRecords($zone) { | |
| $data = 'zone=' . $zone; | |
| return $this->http($this->showRecordURL($this->format), 'GET', $data, $this->to_header()); | |
| } | |
| /** | |
| * Create resource record | |
| * | |
| * @access public | |
| * @param string zone Zone name for the host | |
| * @param string host Host you want to create | |
| * @param string record_type Resource record e.g: A, MX, CNAME, AAAA, TXT and NS | |
| * @param string content Valid content base on resource record | |
| * @param integer prio MX priority. The default value is 10 | |
| * @param integer ttl Time to live in seconds. Default value is 43200 | |
| * @return string API response | |
| */ | |
| public function createRecord($zone, $host, $record_type, $content, $prio = 10, $ttl = 43200) { | |
| $data = array( | |
| 'zone' => $zone, | |
| 'host' => $host, | |
| 'record_type' => $record_type, | |
| 'content' => $content, | |
| 'prio' => $prio, | |
| 'ttl' => $ttl | |
| ); | |
| return $this->http($this->createRecordURL($this->format), 'POST', http_build_query($data), $this->to_header()); | |
| } | |
| /** | |
| * Delete resource record | |
| * | |
| * @access public | |
| * @param string zone Zone name for the host | |
| * @param string host Host you want to create | |
| * @param string record_type Resource record e.g: A, MX, CNAME, AAAA, TXT and NS | |
| * @param string content Valid content base on resource record | |
| * @param integer prio MX priority. The default value is 10 | |
| * @param integer ttl Time to live in seconds. Default value is 43200 | |
| * @return string API response | |
| */ | |
| public function deleteRecord($zone, $host, $record_type, $content, $prio = 10, $ttl = 43200) { | |
| $data = array( | |
| 'zone' => $zone, | |
| 'host' => $host, | |
| 'record_type' => $record_type, | |
| 'content' => $content, | |
| 'prio' => $prio, | |
| 'ttl' => $ttl | |
| ); | |
| return $this->http($this->deleteRecordURL($this->format), 'DELETE', http_build_query($data), $this->to_header()); | |
| } | |
| /** | |
| * Deleting zone | |
| * | |
| * @access public | |
| * @param string zone the zone | |
| * @return string API response | |
| */ | |
| public function deleteZone($zone) { | |
| $data = array('zone' => $zone); | |
| return $this->http($this->zoneURL($this->format), 'DELETE', http_build_query($data), $this->to_header()); | |
| } | |
| /** | |
| * Builds the Authorization header | |
| * | |
| * @access public | |
| * @return string Authorization: header | |
| */ | |
| public function to_header() { | |
| return 'Authorization: Token token="' . $this->token . '"'; | |
| } | |
| /** | |
| * Make a HTTP request | |
| * | |
| * @access public | |
| * @return string API results | |
| */ | |
| public function http($url, $method, $postfields = NULL, $headers = NULL) { | |
| $ci = curl_init(); | |
| /* Curl settings */ | |
| curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent); | |
| curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout); | |
| curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout); | |
| curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE); | |
| curl_setopt($ci, CURLOPT_VERBOSE, TRUE); | |
| curl_setopt($ci, CURLOPT_HTTPHEADER, array($headers)); | |
| curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer); | |
| curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); | |
| curl_setopt($ci, CURLOPT_HEADER, FALSE); | |
| switch ($method) { | |
| case 'GET': | |
| curl_setopt($ci, CURLOPT_HTTPGET, TRUE); | |
| curl_setopt($ci, CURLOPT_POST, FALSE); | |
| if (!empty($postfields)) { | |
| $url = "{$url}?{$postfields}"; | |
| } | |
| break; | |
| case 'POST': | |
| curl_setopt($ci, CURLOPT_HTTPGET, FALSE); | |
| curl_setopt($ci, CURLOPT_POST, TRUE); | |
| if (!empty($postfields)) { | |
| curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); | |
| } | |
| break; | |
| case 'DELETE': | |
| curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
| if (!empty($postfields)) { | |
| $url = "{$url}?{$postfields}"; | |
| } | |
| break; | |
| case 'PUT': | |
| curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
| if (!empty($postfields)) { | |
| curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); | |
| } | |
| break; | |
| } | |
| curl_setopt($ci, CURLOPT_URL, $url); | |
| $response = curl_exec($ci); | |
| curl_close($ci); | |
| return $response; | |
| } | |
| /** | |
| * Get the header info to store. | |
| * | |
| * @access public | |
| * @return integer length | |
| */ | |
| public function getHeader($ch, $header) { | |
| $i = strpos($header, ':'); | |
| if (!empty($i)) { | |
| $key = str_replace('-', '_', strtolower(substr($header, 0, $i))); | |
| $value = trim(substr($header, $i + 2)); | |
| $this->http_header[$key] = $value; | |
| } | |
| return strlen($header); | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment