This repository was archived by the owner on Mar 24, 2022. It is now read-only.
forked from nfacha/dnsimple-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnSimpleApi.php
More file actions
63 lines (51 loc) · 2.01 KB
/
dnSimpleApi.php
File metadata and controls
63 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
use Curl\Curl;
class dnSimpleApi {
private $apiKey;
private $accountId;
private $apiUrl = 'https://api.dnsimple.com/v2/';
/**
* dnSimpleApi constructor.
*
* @param $apiKey
* @param $accountId
*/
public function __construct( $apiKey, $accountId ) {
$this->apiKey = $apiKey;
$this->accountId = $accountId;
}
private function getHeaders() {
return [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->apiKey
];
}
public function listRecords( $domainName ) {
$curl = new Curl();
$curl->setHeaders( $this->getHeaders() );
$curl->get( $this->apiUrl . '/' . $this->accountId . '/zones/' . $domainName . '/records' );
if ( $curl->httpStatusCode !== 200 ) {
throw new Exception( 'Failed to list DNS zones, got ' . $curl->httpStatusCode );
}
return json_decode( json_encode( $curl->response ), true );
}
public function createRecord( $domainName, $recordConfig ) {
$curl = new Curl();
$curl->setHeaders( $this->getHeaders() );
$curl->post( $this->apiUrl . '/' . $this->accountId . '/zones/' . $domainName . '/records', json_encode( $recordConfig ) );
if ( $curl->httpStatusCode !== 201 ) {
throw new Exception( 'Failed to create DNS zone, got ' . $curl->httpStatusCode );
}
return json_decode( json_encode( $curl->response ), true );
}
public function deleteRecord( $domainName, $recordId ) {
$curl = new Curl();
$curl->setHeaders( $this->getHeaders() );
$curl->delete( $this->apiUrl . '/' . $this->accountId . '/zones/' . $domainName . '/records/' . $recordId );
if ( $curl->httpStatusCode !== 204 ) {
throw new Exception( 'Failed to delete DNS zone, got ' . $curl->httpStatusCode );
}
return json_decode( json_encode( $curl->response ), true );
}
}