PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/namecheap.php

https://github.com/cstrouse/Namecheap-REST-PHP
PHP | 373 lines | 231 code | 22 blank | 120 comment | 34 complexity | 748030971f7024b1cc26bbaec0ff174d MD5 | raw file
  1. <?php
  2. // Namecheap API class
  3. class namecheap
  4. {
  5. // API credential information required to execute requests
  6. private $api_url;
  7. private $api_user;
  8. private $api_key;
  9. private $api_ip;
  10. // storage for API responses
  11. public $Response;
  12. public $Error;
  13. public $Raw;
  14. /*
  15. * instantiate a namecheap object
  16. * @credentials array associative array of namecheap API credentials
  17. * @sandbox boolean whether to use the Namecheap Sandbox or the real site
  18. * @return object a namecheap object
  19. */
  20. public function __construct( $credentials, $sandbox = true )
  21. {
  22. if ( $sandbox ) {
  23. $this->api_url = 'https://api.sandbox.namecheap.com/xml.response';
  24. } else {
  25. $this->api_url = 'https://api.namecheap.com/xml.response';
  26. }
  27. $this->api_user = $credentials['api_user'];
  28. $this->api_key = $credentials['api_key'];
  29. $this->api_ip = trim(( 'detect' == $credentials['api_ip'] ) ? $this->detect_ip() : $credentials['api_ip']);
  30. }
  31. /*
  32. * determine our IP address
  33. * @return string our public IP address, as seen by icanhazip.com
  34. */
  35. public function detect_ip()
  36. {
  37. $ch = curl_init( 'http://icanhazip.com' );
  38. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
  39. $result = curl_exec( $ch );
  40. curl_close( $ch );
  41. return $result;
  42. }
  43. /*
  44. * execute a call to the Namecheap API
  45. * @command string the name of the API call to invoke
  46. * @args array associative array of options for the API call
  47. * @return bool success or failure
  48. */
  49. private function execute( $command, $args = array() )
  50. {
  51. // blank out any previous values for these
  52. $this->Error = '';
  53. $this->Response = '';
  54. $this->Raw = '';
  55. $url = $this->api_url .
  56. '?ApiUser=' . $this->api_user .
  57. '&ApiKey=' . $this->api_key .
  58. '&UserName=' . $this->api_user .
  59. '&ClientIP=' . $this->api_ip .
  60. '&Command=' . $command;
  61. foreach ( $args as $arg => $value )
  62. {
  63. $url .= "&$arg=";
  64. $url .= urlencode( $value );
  65. }
  66. $ch = curl_init( $url );
  67. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
  68. $result = curl_exec( $ch );
  69. curl_close( $ch );
  70. if ( FALSE == $result ) {
  71. $this->Error = 'Communication error with Namecheap.';
  72. return FALSE;
  73. }
  74. $xml = new SimpleXMLElement( $result );
  75. $this->Raw = $xml;
  76. if ( 'ERROR' == $xml['Status'] )
  77. {
  78. $this->Error = (string) $xml->Errors->Error;
  79. return FALSE;
  80. } elseif ( 'OK' == $xml['Status'] )
  81. {
  82. $this->Response = $xml->CommandResponse;
  83. return true;
  84. }
  85. }
  86. /*
  87. * check the availability of one or more domains
  88. * @domains mixed array, comma delimited list, or single domain name
  89. * @return mixed associative array of domains => status,
  90. * or boolean if only a single domain is being checked
  91. */
  92. public function domainsCheck( $domains )
  93. {
  94. if ( is_array( $domains ) ) {
  95. $domains = implode( ',', $domains );
  96. }
  97. if ( ! $this->execute( 'namecheap.domains.check', array( 'DomainList' => $domains ) ) ) {
  98. // communication error
  99. return FALSE;
  100. }
  101. if ( FALSE === strpos( $domains, ',' ) ) {
  102. //only one domain was passed, so just return
  103. // the availability of that domain
  104. $status = ( 'true' == strtolower( (string)$this->Response->DomainCheckResult->attributes()->Available ) ) ? TRUE : FALSE;
  105. return $status;
  106. }
  107. $r = array();
  108. foreach ( $this->Response->DomainCheckResult as $result ) {
  109. $domain = (string)$result['Domain'];
  110. $status = ( 'true' == strtolower( (string)$result['Available'] ) ) ? TRUE : FALSE;
  111. $r[$domain] = $status;
  112. }
  113. return $r;
  114. }
  115. /*
  116. * register a domain
  117. * @domain string the domain name to register
  118. * @data array associative array of required registration data
  119. * http://developer.namecheap.com/docs/doku.php?id=api-reference:domains:create
  120. * @return bool success or failure of the registration
  121. */
  122. public function domainsCreate( $domain, $data )
  123. {
  124. $data['DomainName'] = $domain;
  125. if ( ! $this->execute( 'namecheap.domains.create', $data ) ) {
  126. return FALSE;
  127. }
  128. if ( 'true' == strtolower( $this->Response->DomainCreateResult->attributes()->Registered ) )
  129. {
  130. return TRUE;
  131. }
  132. return FALSE;
  133. }
  134. /*
  135. * return a list of contact info for the domain specified
  136. * @return array of contacts or boolean false
  137. */
  138. public function domainsGetContacts($domain)
  139. {
  140. $this->execute('namecheap.domains.getContacts', array('DomainName' => $domain));
  141. // TODO: Handle the results
  142. }
  143. /*
  144. * return a list of domains we own
  145. * @type string All, Expiring, or Expired. Defaults to All
  146. * @page int page number to return
  147. * @pagesize int number of domains per page. minimum 10, max 100.
  148. * @sort string NAME, NAME_DESC, EXPIREDATE, EXPIREDATE_DESC, CREATEDATE, CREATEDATE_DESC
  149. * @search string specific name for which to search
  150. * @return mixed an array of domains or boolean false
  151. */
  152. public function domainsGetList( $type = 'all', $page = 1, $pagesize = 100, $sort = 'NAME', $search = '')
  153. {
  154. if ( ! $this->execute( 'namecheap.domains.getList', array( 'ListType' => $type, 'SearchTerm' => $search, 'Page' => $page, 'PageSize' => $pagesize, 'SortBy' => $sort ) ) ) {
  155. return FALSE;
  156. }
  157. $domains = array();
  158. foreach ( $this->Response->DomainGetListResult->Domain as $domain ) {
  159. $x = array();
  160. foreach( $domain->attributes() as $k => $v ) {
  161. $x[$k] = (string)$v;
  162. }
  163. $domains[] = $x;
  164. }
  165. return $domains;
  166. }
  167. /*
  168. * gets the status of RegistrarLock for the requested domain
  169. * @domain string the domain to query
  170. * @return boolean or error
  171. */
  172. public function domainsGetRegistrarLock($domain)
  173. {
  174. $this->execute('namecheap.domains.getRegistrarLock', array('DomainName' => $domain));
  175. if('true' == strtolower($this->Response->DomainGetRegistrarLockResult->attributes()->RegistrarLockStatus))
  176. {
  177. return TRUE;
  178. }
  179. return FALSE;
  180. }
  181. /*
  182. * return a list of TLDs
  183. */
  184. public function domainsGetTldList()
  185. {
  186. $this->execute('namecheap.domains.getTldList');
  187. // TODO: Handle result
  188. }
  189. /*
  190. * reactive a domain
  191. * @return boolean true on success, boolean false on failure
  192. */
  193. public function domainsReactivate($domain)
  194. {
  195. $this->execute('namecheap.domains.reactivate', array('DomainName' => $domain));
  196. // TODO: Handle response
  197. }
  198. /*
  199. * renew an expiring domain
  200. * @return boolean true on success, boolean false on failure
  201. */
  202. public function domainsRenew($domain, $years, $promo_code)
  203. {
  204. // TODO: Write me
  205. }
  206. /*
  207. * sets contact information for the requested domain
  208. * @return boolean true on success, boolean false on failure
  209. */
  210. public function domainsSetContacts($domain, $data)
  211. {
  212. // TODO: Write me
  213. }
  214. /*
  215. * sets the RegistrarLock status for a domain
  216. *
  217. */
  218. public function domainsSetRegistrarLock($domain, $lock_action)
  219. {
  220. $this->execute('namecheap.domains.setRegistarLock', array('DomainName' => $domain, 'LockAction' => $lock_action));
  221. // TODO: Handle response
  222. }
  223. /*
  224. * create new nameservers
  225. * @domain string domain name to which these nameservers will be assigned
  226. * @nameserver string the FQDN of the nameserver to create
  227. * @ip string the IP address of the nameserver to create
  228. * @return bool success or failure
  229. */
  230. public function nsCreate( $domain, $nameserver, $ip )
  231. {
  232. list( $sld, $tld ) = explode( '.', $domain );
  233. $args['SLD'] = $sld;
  234. $args['TLD'] = $tld;
  235. $args['Nameserver'] = $nameserver;
  236. $args['IP'] = $ip;
  237. if ( $this->execute( 'namecheap.domains.ns.create', $args ) ) {
  238. return TRUE;
  239. }
  240. return $false;
  241. }
  242. /*
  243. * assign nameservers to a domain
  244. * @domain string the domain name that will be assigned nameservers
  245. * @nameservers mixed an array or comma delimited list of nameservers
  246. * @return bool success or failure
  247. */
  248. public function dnsSetCustom( $domain, $nameservers )
  249. {
  250. if ( is_array( $nameservers ) ) {
  251. $nameservers = implode( ',', $nameservers );
  252. }
  253. list( $sld, $tld ) = explode( '.', $domain );
  254. $args['SLD'] = $sld;
  255. $args['TLD'] = $tld;
  256. $args['NameServers'] = $nameservers;
  257. if ( ! $this->execute( 'namecheap.domains.dns.setCustom', $args ) ) {
  258. return FALSE;
  259. }
  260. if ( 'true' == strtolower( $this->Response->DomainDNSSetCustomResult->attributes()->Updated ) )
  261. {
  262. return TRUE;
  263. }
  264. return FALSE;
  265. }
  266. /*
  267. * configure a domain to use Namecheap's default nameservers
  268. * @domain string the domain to set
  269. * @return bool success or failure
  270. */
  271. public function dnsSetDefault( $domain )
  272. {
  273. list( $sld, $tld ) = explode( '.', $domain );
  274. if ( ! $this->execute( 'namecheap.domains.dns.SetDefault', array( 'SLD' => $sld, 'TLD' => $tld ) ) ) {
  275. return FALSE;
  276. }
  277. if ( 'true' == strtolower( $this->Response->DomainDNSSetDefaultResult->attributes()->Updated ) ) {
  278. return TRUE;
  279. }
  280. return FALSE;
  281. }
  282. /*
  283. * get a list of DNS servers for a domain
  284. * @domain string the domain to query
  285. * @return mixed an array of nameservers, or boolean false
  286. */
  287. public function dnsGetList( $domain )
  288. {
  289. list( $sld, $tld ) = explode( '.', $domain );
  290. if ( ! $this->execute( 'namecheap.domains.dns.getList', array( 'SLD' => $sld, 'TLD' => $tld ) ) ) {
  291. return FALSE;
  292. }
  293. $servers = array();
  294. foreach ( $this->Response->DomainDNSGetListResult->Nameserver as $ns ) {
  295. $servers[] = (string)$ns;
  296. }
  297. return $servers;
  298. }
  299. /*
  300. * set DNS host records for the specified domain
  301. * @domain string domain for which the record should be defined
  302. * @data array associative array of record details to set
  303. * @return bool success or failure
  304. */
  305. public function dnsSetHosts( $domain, $data )
  306. {
  307. list( $data['SLD'], $data['TLD'] ) = explode( '.', $domain );
  308. if ( ! $this->execute( 'namecheap.domains.dns.setHosts', $data ) ) {
  309. return FALSE;
  310. }
  311. if ( 'true' == strtolower( $this->Response->DomainDNSSetHostsResult->attributes()->IsSuccess ) ) {
  312. return TRUE;
  313. }
  314. return FALSE;
  315. }
  316. /*
  317. * return the balance of an account
  318. * @return mixed an array of balance information, or boolean false
  319. */
  320. public function usersGetBalances()
  321. {
  322. if( ! $this->execute( 'namecheap.users.getBalances' ) ) {
  323. return FALSE;
  324. }
  325. foreach ( $this->Response->UserGetBalancesResult->attributes() as $k => $v ) {
  326. $balance[$k] = (string)$v;
  327. }
  328. return $balance;
  329. }
  330. /*
  331. * return pricing information for TLDs
  332. * @type string one of DOMAIN, SSLCERTIFICATE or WHOISGUARD
  333. * @category string specific category within product type
  334. * @promo string promotional code
  335. * http://developer.namecheap.com/docs/doku.php?id=api-reference:users:getpricing
  336. * @return mixed
  337. */
  338. public function getPricing( $type = 'DOMAIN', $category ='', $promo = '' )
  339. {
  340. $args = array( 'ProductType' => $type );
  341. if ( ! empty( $category ) )
  342. {
  343. $args['ProductCategory'] = $category;
  344. }
  345. if ( ! empty( $promo ) )
  346. {
  347. $args['PromotionCode'] = $promo;
  348. }
  349. $this->execute( 'namecheap.users.getPricing', $args );
  350. }
  351. }