-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGithub.php
229 lines (197 loc) · 4.66 KB
/
Github.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
<?php
class Github {
/**
* API options
*/
private $_url = 'https://api.github.com';
private $_username = null;
private $_password = null;
/**
* Errors
*/
private $_errors = array();
/**
* Pagination
*/
public $page = null;
public $perPage = null;
/**
* Construct with github account
*
* @param string $username Default: null
* @param string $password Default: null
*/
public function __construct($username = null, $password = null) {
$this->_username = $username;
$this->_password = $password;
}
/**
* Reset the pagination options
*/
private function _resetPagination() {
$this->page = null;
$this->perPage = null;
}
/**
* Construct complete url
*
* @param string $ressource
* @param array $params Default: array()
*/
private function _parseUrl($ressource, $params = array()) {
$set = false;
$url = $this->_url;
$url .= $ressource;
if (!is_null($this->page)) {
$set = true;
$url .= '?page='.$this->page;
if (!is_null($this->perPage)) {
$url .= '&per_page='.$this->perPage;
}
}
elseif (!is_null($this->perPage)) {
$set = true;
$url .= '?per_page='.$this->perPage;
}
if (is_array($params) and (!empty($params))) {
foreach ($params as $k => $v) {
if ($set) {
$url .= '&';
}
else {
$set = true;
$url .= '?';
}
$url .= $k.'='.$v;
}
}
$this->_resetPagination();
return $url;
}
private function _parseParams($params = array()) {
if (is_array($params) and !empty($params)) {
$return = array();
foreach ($params as $k => $v) {
if ($v !== false) {
$return[$k] = $v;
}
}
}
else {
$return = $params;
}
return $return;
}
/**
* Add error to error list
*
* @param string $url Request url
* @param string $message Error message. Default: ''
* @param int $code HTTP error code. Default: 0
* @param array $errors Error details. Default: array()
* @param array $params Request params. Default: array()
*/
public function addError($url, $message = '', $code = 0, $errors = array(), $params = array(), $method) {
$this->_errors[] = array(
'url' => $url,
'message' => $message,
'code' => $code,
'errors' => $errors,
'params' => $params,
'method' => $method,
);
}
/**
* Return all errors
*/
public function getErrors() {
return $this->_errors;
}
/**
* Check if a error exists
*/
public function hasError() {
return (!empty($this->_errors));
}
/**
* Exectute curl request
*
* @param string $ressource
* @param string $method Default: GET
* @param array $params Default: array
* @param boolean $code Return status code
*/
public function request($ressource = '', $method = 'GET', $params = array(), $code = false) {
//Parse params
$params = $this->_parseParams($params);
//Parse url
if ($method == 'GET') {
$url = $this->_parseUrl($ressource, $params);
}
else {
$url = $this->_parseUrl($ressource);
}
$handle = curl_init();
//Set options
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
//Set username and password
if (!is_null($this->_username) and !is_null($this->_password)) {
curl_setopt($handle, CURLOPT_USERPWD, $this->_username.':'.$this->_password);
}
//Set method and params
switch ($method) {
case 'GET':
break;
case 'PUT':
case 'DELETE':
case 'POST':
case 'PATCH':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($params));
break;
default:
$this->addError('Unknown method '.$method);
return false;
break;
}
//Execute
$buffer = curl_exec($handle);
$info = curl_getinfo($handle);
curl_close($handle);
$response = json_decode($buffer);
//Return
if ($code) {
return $info['http_code'];
}
else {
if (!is_array($response) and isset($response->message)) {
$errors = isset($response->errors) ? $response->errors : array();
$this->addError($url, $response->message, $info['http_code'], $errors, $params, $method);
return false;
}
else {
return $response;
}
}
}
/**
* Check rate limit. This check does not increase the limit
*
* @return array Remaining API calls and the limit per hour
*/
public function rateLimit() {
$response = $this->request('/rate_limit');
if (($response !== false) and (isset($response->rate) and (isset($response->rate->remaining)) and (isset($response->rate->limit)))) {
return array(
'remaining' => $response->rate->remaining,
'limit' => $response->rate->limit,
);
}
else {
return false;
}
}
}
?>