-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdd-api.php
156 lines (130 loc) · 4.4 KB
/
pdd-api.php
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/*
Yandex API for PDD by Pasha1st
2018-04-23 v. 0.1 first release
*/
class HTTPException extends Exception {};
class YandexAPICore {
protected $pddToken;
protected $url='https://pddimp.yandex.ru';
protected $userAgent='pfYaAPIBot/1.0';
protected $curl;
protected $timeout=30;
function __construct($token,$url=null) {
$this->pddToken=$token;
if (!is_null($url)) $this->url=$url;
$this->curl=curl_init($this->url);
if (!$this->curl) throw new Exception('curl_init() failed');
}
public function sendQuery($path, $params, $method='GET') {
$urlparams='';
if (is_array($params)) {
foreach($params as $k=>$v) {
$urlparams.=(($urlparams==''?'':'&')).urlencode($k).'='.urlencode($v);
};
} else
$urlparams=$params;
curl_setopt_array($this->curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CONNECTTIMEOUT => $this->timeout,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_USERAGENT => $this->userAgent,
CURLOPT_HTTPHEADER => array('PddToken: '.$this->pddToken),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS=>'',
// CURLOPT_VERBOSE => true
)
);
if ($method=='GET') {
if ($urlparams!='') $urlparams='?'.$urlparams;
curl_setopt_array($this->curl, array(
CURLOPT_URL => $this->url.'/'.$path.$urlparams,
CURLOPT_HTTPGET => true,
)
);
} elseif($method=='POST') {
curl_setopt_array($this->curl, array(
CURLOPT_URL => $this->url.'/'.$path,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS=>$urlparams
)
);
// print "Params: $urlparams\r\n";
} else {
curl_setopt_array($this->curl, array(
CURLOPT_URL => $this->url.'/'.$path.$urlparams,
CURLOPT_CUSTOMREQUEST=>$method
)
);
};
$res=curl_exec($this->curl);
if ($res===false) throw new Exception('curl_exec() failed');
$httpCode=curl_getinfo($this->curl,CURLINFO_HTTP_CODE);
if ($httpCode!=200) throw new HTTPException('HTTP request failed',$httpCode);
return $res;
}
public function getDomains($page=-1,$limit=-1) {
$params=array();
if ($page>0) $params['page']=$page;
if ($limit>0) $params['on_page']=$limit;
$answer=$this->sendQuery('/api2/admin/domain/domains',$params);
return json_decode($answer,false);
}
}
/*
All methods return json-decoded objects
*/
class YandexDNS {
protected $core;
function __construct($token) {
$this->core=new YandexAPICore($token);
}
public function getCore() {return $this->core;}
public function getDNSRecords($domain) {
$answer=$this->core->sendQuery('/api2/admin/dns/list',array('domain'=>$domain));
return json_decode($answer,false);
}
public function addDNSRecord($domain,$record) {
// $params=array();
$params=$record;
$params['domain']=$domain;
$answer=$this->core->sendQuery('/api2/admin/dns/add',$params,'POST');
return json_decode($answer,false);
}
public function editDNSRecord($domain,$record_id,$record) {
// $params=array();
$params=$record;
$params['domain']=$domain;
$params['record_id']=$record_id;
$answer=$this->core->sendQuery('/api2/admin/dns/edit',$params,'POST');
return json_decode($answer,false);
}
public function delDNSRecord($domain,$record_id) {
// $params=array();
$params=array();
$params['domain']=$domain;
$params['record_id']=$record_id;
$answer=$this->core->sendQuery('/api2/admin/dns/del',$params,'POST');
return json_decode($answer,false);
}
/*
Filter records
IN: result-object from getDNSRecords()
IN: filter - array(property=>value,...)
OUT: filtered $answer->records
*/
static public function filterDNSRecords($answer,$filter) {
if ($answer->success!='ok') return false;
$res=array_filter($answer->records,
function($val) use ($filter) {
$res=true;
foreach($filter as $k=>$v)
$res=$res && ($val->$k==$v);
return $res;
}
);
return $res;
}
}
?>