-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckan.php
executable file
·237 lines (205 loc) · 6.29 KB
/
ckan.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
class Ckan {
private $url = 'CKAN_URL';
private $apikey = 'API_KEY';
private $debugmode = FALSE;
private $errors = array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict (e.g. name already exists)',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Large',
415 => 'Unsupported Media Type',
416 => 'Requested range not satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
);
public function __construct($url=null, $apikey=null){
if ($url){ $this->url=$url; }
if ($apikey) { $this->api_key = $apikey; }
}
private function transfer($url, $method='GET', $data=null){
$ch = curl_init($this->url . $url);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: ' .$this->api_key)
);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
print_r($info);
curl_close($ch);
if ($info['http_code'] != 200){
throw new CkanException($this->error_codes["$info[http_code]"]);
}
if (!$result){
throw new CkanException("No Result");
}
return json_decode($result);
}
private function actiontransfer($url,$data){
$data_string = json_encode($data);
$ch = curl_init($this->url. $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1) ;
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'Authorization: ' .$this->api_key)
);
$result = curl_exec($ch);
//print_r($result);
$info = curl_getinfo($ch);
//print_r($info);
curl_close($ch);
return json_decode($result);
}
public function search($keyword){
$results = $this->transfer('api/search/package/?all_fields=1&q=' . urlencode($keyword));
if (!$results->count){
throw new CkanException("Search Error");
}
return $results;
}
public function getPackage($data){
$package = $this->actiontransfer('api/action/datalocale_package_show',$data);
// print_r($package);
if (!is_array($package) && !is_object($package)){
throw new CkanException("Package Load Error".$data['id']);
}
return $package;
}
public function gettags($data){
$tags = $this->actiontransfer('api/action/tag_list',$data);
if (!is_array($tags) && !is_object($tags)){
throw new CkanException("Tags Load Error");
}
return $tags;
}
public function getsoustags($data){
$tags = $this->actiontransfer('api/action/datalocale_vocabulary_show',$data);
if (!is_array($tags) && !is_object($tags)){
throw new CkanException("Tags Load Error");
}
return $tags;
}
public function gettagsdatalocale($data){
$tags = $this->actiontransfer('api/action/datalocale_tag_list',$data);
if (!is_array($tags) && !is_object($tags)){
throw new CkanException("Tags Load Error");
}
return $tags;
}
public function tagscat($data){
$tags = $this->actiontransfer('api/action/datalocale_vocabulary_list',$data);
if (!is_array($tags) && !is_object($tags)){
throw new CkanException("Tags Load Error");
}
return $tags;
}
public function getckanusers($data){
$users = $this->actiontransfer('api/action/user_list',$data);
if (!is_array($users) && !is_object($users)){
throw new CkanException("User List Error");
}
return $users;
}
public function getUser($data){
$users = $this->actiontransfer('api/action/user_show',$data);
if (!is_array($users) && !is_object($users)){
throw new CkanException("User show Error");
}
return $users;
}
public function getckanuserrole($data){
$role = $this->actiontransfer('api/action/datalocale_role_user',$data);
if (!is_array($role) && !is_object($role)){
throw new CkanException("User List Error");
}
return $users;
}
public function getckanpackagerole($data){
$role = $this->actiontransfer('api/action/datalocale_show_roles',$data);
if (!is_array($role) && !is_object($role)){
throw new CkanException("User List Error");
}
return $users;
}
public function getPackageList($data){
// $list = $this->actiontransfer('api/action/datalocale_package_list',$data);
$list = $this->actiontransfer('api/action/package_list',$data);
// print_r($list);
if (!is_array($list) && !is_object($list)){
throw new CkanException("Package List Error");
}
return $list;
}
public function getGroup($data){
$group = $this->actiontransfer('api/action/datalocale_group_show' , $data );
if (!is_array($group) && !is_object($group)){
throw new CkanException("Group Error");
}
return $group;
}
public function getGroupList(){
$groupList = $this->transfer('api/rest/group/');
print_r($groupList);
if (!is_array($groupList)){
throw new CkanException("Group List Error");
}
return $groupList;
}
public function getLicenseList(){
$list = $this->transfer('api/rest/licenses');
if (!is_array($list)){
throw new CkanException("License List Error");
}
return $list;
}
public function getRevisionsSinceTime($time){
$time = strftime("%Y-%m-%dT%H:%M:%S", $time);
$revisionList = $this->transfer('api/search/revision?since_time=' . $time, $this->no_result_means_array );
return $revisionList;
}
public function getRevision($revisionID){
$revision = $this->transfer('api/rest/revision/' . urlencode($revisionID) );
return $revision;
}
}
//api/tag_counts
class CkanException extends Exception{}
?>