Files
thetool/lib/Inwx/Inwx.php
2024-03-12 21:24:56 +01:00

156 lines
4.0 KiB
PHP

<?php
/**
* Represents the INWX API.
*
* @link https://www.inwx.at/en/help/apidoc JSON-RPC API documentation
*/
class Inwx {
private string $apiUrl = 'https://api.domrobot.com/jsonrpc/';
private string $username;
private string $password;
/**
* Inwx constructor.
*
* @param string $username
* @param string $password
*/
public function __construct(string $username, string $password) {
$this->username = $username;
$this->password = $password;
}
/**
* Get the list of contacts.
*
* @param int $pageLimit
* @return array
* @throws Exception
*/
public function contactList(int $pageLimit = 300): array {
$requestData = array(
'jsonrpc' => '2.0',
'method' => 'contact.list',
'params' => array(
'user' => $this->username,
'pass' => $this->password,
'pagelimit' => $pageLimit,
),
'id' => 1
);
return $this->makeRequest($requestData)['contact'];
}
/**
* Get the list of domains.
*
* @param int $pageLimit
* @return array
* @throws Exception
*/
public function domainList(int $pageLimit = 300): array {
$requestData = array(
'jsonrpc' => '2.0',
'method' => 'domain.list',
'params' => array(
'user' => $this->username,
'pass' => $this->password,
'pagelimit' => $pageLimit,
),
'id' => 1
);
return $this->makeRequest($requestData)['domain'];
}
/**
* Check if a domain is available.
*
* @param string $domain
* @param string $tld
* @return array
* @throws Exception
*/
public function domainCheck(string $domain): array {
$requestData = array(
'jsonrpc' => '2.0',
'method' => 'domain.check',
'params' => array(
'user' => $this->username,
'pass' => $this->password,
'domain' => $domain
),
'id' => 1
);
return $this->makeRequest($requestData);
}
/**
* Get the price of a domain.
*
* @param string $domain
* @param string $priceType reg | renewal | transfer | update | trade | restore
* @return array
* @throws Exception
*/
public function domainGetDomainPrice(string $domain, string $priceType): array {
$requestData = array(
'jsonrpc' => '2.0',
'method' => 'domain.getdomainprice',
'params' => array(
'user' => $this->username,
'pass' => $this->password,
'domain' => $domain,
'pricetype' => $priceType,
),
'id' => 1
);
return $this->makeRequest($requestData);
}
/**
* Make a request to the INWX API.
*
* @param array $requestData
* @return array
* @throws Exception
*/
private function makeRequest(array $requestData): array {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
$response = curl_exec($ch);
if ($response === false) {
throw new Exception('cURL Error: ' . curl_error($ch));
}
curl_close($ch);
$responseData = json_decode($response, true);
if (isset($responseData['error'])) {
throw new Exception('JSON-RPC Error: ' . $responseData['error']['message']);
}
if (!isset($responseData['resData'])) {
throw new Exception('Unexpected response format.');
}
return $responseData['resData'];
}
}
?>