Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 316 lines (251 sloc) 10.958 kb
47d84a8 @Kumnaa Added the erep api class.
authored
1 <?php
2
3 /*
4 Erepublik API client
5
6 @author Ben Bowtell
7
8 @date 27-Feb-2011
9
10 (c) 2011 by http://www.amplifycreative.net
11
12 contact: ben@amplifycreative.net.net
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28 /* INSTRUCTIONS
29
30 To initialise:
31
32 $erep = new erep_api(<consumer key>, <consumer secret>);
33
34 To set the callback url:
35
36 $erep->set_callback(<callback url>);
37
38 To get access key for 'citizen authentcation' and 'debit citizen account':
39
40 $erep->request_access(<scope>);
41
42 // returns the oauth token as a string or on error it returns false
43 <scope> examples:
44 to authenticate citizen and get wellness 'citizen/info'
45
46 Once you have an oauth_token......
47
48 To auth citizen:
49
50 $erep->get_citizen_data(<oauth_token>, <oauth_verifier>);
51
52 // returns citizen xml as a simplexml object or on error it throws an exception
53
54 To debug:
55
56 echo $erep->debug(string);
57
58 // ***** these are no longer used and are just here incase they come back *****
59
60 To debit citizen account:
61
62 $erep->debit_citizen(<oauth_token, <oauth_verifier>);
63
64 // returns debit xml as a simplexml object on error it throws an exception
65
66 To credit citizen account:
67
68 $erep->credit_citizen(<citizen id>, <amount>[, <currency>]);
69
70 // returns credit xml as a simplexml object or on error it returns error simplexml object
71 // <currency> defaults to Gold
72 */
73
74 class erep_api {
75
76 private $params;
a82159d @Kumnaa Erep api class tidy up.
authored
77 private $request_method = 'GET';
47d84a8 @Kumnaa Added the erep api class.
authored
78 private $request_url;
79 private $secret;
80 private $normalised_string;
81 private $signature;
82 private $scope;
83 private $consumer_secret;
a82159d @Kumnaa Erep api class tidy up.
authored
84 private $url_domain = 'http://api.erepublik.com';
47d84a8 @Kumnaa Added the erep api class.
authored
85 private $subject;
86 private $api_timeout = 20; // set the api timeout in seconds
a82159d @Kumnaa Erep api class tidy up.
authored
87 private $debug_string = '';
47d84a8 @Kumnaa Added the erep api class.
authored
88 private $callback;
a82159d @Kumnaa Erep api class tidy up.
authored
89 private $api_url = 'http://api.erepublik.com/oauth/authorize';
47d84a8 @Kumnaa Added the erep api class.
authored
90
91 // constructor
92 public function __construct($consumer_key, $consumer_secret) {
93 $banned_user_agents = array(
94 'Mediapartners-Google'
95 );
a82159d @Kumnaa Erep api class tidy up.
authored
96
ff2aa31 @Kumnaa Added curl checker to erep_api.php.
authored
97 if (function_exists('curl_init') == false) {
a82159d @Kumnaa Erep api class tidy up.
authored
98 throw new Exception('This class needs curl');
99 }
100
47d84a8 @Kumnaa Added the erep api class.
authored
101 if (in_array($_SERVER['HTTP_USER_AGENT'], $banned_user_agents)) {
102 throw new Exception("Banned user agent");
103 }
a82159d @Kumnaa Erep api class tidy up.
authored
104
47d84a8 @Kumnaa Added the erep api class.
authored
105 $this->consumer_secret = $consumer_secret;
106 $this->params = array(
107 'oauth_consumer_key' => $consumer_key,
108 'oauth_nonce' => '',
109 'oauth_signature_method' => 'HMAC-SHA1',
110 'oauth_timestamp' => '',
111 'oauth_token' => '',
112 'oauth_version' => '1.0'
113 );
a82159d @Kumnaa Erep api class tidy up.
authored
114
47d84a8 @Kumnaa Added the erep api class.
authored
115 $this->gen_secret();
116 }
117
118 public function set_callback($url) {
119 $this->callback = $url;
120 }
121
122 // debit the amount declared in the original scope from citizens account and return the xml
123 public function debit_citizen($oauth, $verifier = '') {
124 // get access token
125 $this->params['oauth_token'] = $oauth;
126 if (strlen($verifier) > 0) {
127 $this->params = array_merge($this->params, array('oauth_verifier' => $verifier));
128 }
a82159d @Kumnaa Erep api class tidy up.
authored
129
47d84a8 @Kumnaa Added the erep api class.
authored
130 $data = $this->get_access();
131 // get returned xml from transaction
132 $this->subject = 'citizen';
133 $this->gen_request_url('debit_account');
134 $this->params['oauth_token'] = $data[0];
135 $this->gen_secret($data[1]);
136 $data = $this->api_comm();
137 $data = $this->handle_xml($data);
138 return ($data);
139 }
140
141 // credit $amount of $currency (default gold) to $citizen - no authing required
142 public function credit_citizen($citizen, $amount, $currency = 'Gold') {
143 unset($this->params['oauth_token']);
144 $this->subject = 'citizen';
145 $this->gen_request_url('credit_account');
146 $this->params['currency'] = $currency;
147 $this->params['value'] = $amount;
148 $this->params['citizen_id'] = $citizen;
149 $data = $this->api_comm();
150 $data = $this->handle_xml($data);
151 return ($data);
152 }
153
154 // fetch the data declared in the scope from citizen
155 public function get_citizen_data($oauth, $verifier = '') {
156 $this->params['oauth_token'] = $oauth;
157 if (strlen($verifier) > 0) {
158 $this->params = array_merge($this->params, array('oauth_verifier' => $verifier));
159 }
a82159d @Kumnaa Erep api class tidy up.
authored
160
47d84a8 @Kumnaa Added the erep api class.
authored
161 // get access tokens
162 $data = $this->get_access();
163 // get returned xml from transaction
164 $this->subject = 'citizen';
165 $this->gen_request_url('info');
166 $this->params['oauth_token'] = $data[0];
167 $this->gen_secret($data[1]);
168 $data = $this->api_comm();
169 $data = $this->handle_xml($data);
170 return ($data);
171 }
172
173 // get the request token - used to auth before debiting/gettting citizen info
174 public function request_access($scope) {
175 $this->subject = 'oauth';
176 $this->gen_request_url('request_token');
177 $this->scope = $scope;
178 $this->debug_string .= '<b>Scope:</b> ' . $scope . '<br /><br />';
179 $token = $this->get_token();
180 if ($token == false) {
181 $return = false;
182 } else {
183 $return = $this->api_url . '?oauth_token=' . $token[0];
184 }
a82159d @Kumnaa Erep api class tidy up.
authored
185
47d84a8 @Kumnaa Added the erep api class.
authored
186 return ($return);
187 }
188
189 // swap the request token for the access token
190 private function get_access() {
191 $this->subject = 'oauth';
192 $this->gen_request_url('access_token');
193 $data = $this->get_token();
194 return ($data);
195 }
196
197 // fetch token and secret
198 private function get_token() {
199 $data = $this->api_comm();
200 preg_match('/^oauth_token=([A-Za-z0-9]+)&oauth_token_secret=([A-Za-z0-9]+)$/', $data, $matches);
201 if (count($matches) != 3) {
202 preg_match('/^oauth_token=([A-Za-z0-9]+)&oauth_token_secret=([A-Za-z0-9]+)&oauth_callback_confirmed=true$/', $data, $matches);
203 }
a82159d @Kumnaa Erep api class tidy up.
authored
204
47d84a8 @Kumnaa Added the erep api class.
authored
205 if (count($matches) > 0) {
206 $auth = $matches[1];
207 $secret = $matches[2];
208 $return = array($auth, $secret);
209 } else {
210 $return = $this->handle_xml($data);
211 }
a82159d @Kumnaa Erep api class tidy up.
authored
212
47d84a8 @Kumnaa Added the erep api class.
authored
213 return($return);
214 }
215
216 // general function to communcate with the api and return the output
217 private function api_comm() {
218 $this->params['oauth_nonce'] = md5(rand(1, 100));
219 $this->params['oauth_timestamp'] = time();
220 if (strlen($this->callback) > 0) {
221 $this->params = array_merge($this->params, array('oauth_callback' => $this->callback));
222 }
a82159d @Kumnaa Erep api class tidy up.
authored
223
47d84a8 @Kumnaa Added the erep api class.
authored
224 if (strlen($this->scope) > 0) {
225 $this->params = array_merge($this->params, array('scope' => $this->scope));
226 }
a82159d @Kumnaa Erep api class tidy up.
authored
227
47d84a8 @Kumnaa Added the erep api class.
authored
228 $this->signature = $this->gen_signature();
229 $parameters = $this->normalised_string . '&oauth_signature=' . urlencode($this->signature);
230 if ($this->request_method != "POST") {
231 $this->request_url .= '?' . $parameters;
232 }
a82159d @Kumnaa Erep api class tidy up.
authored
233
47d84a8 @Kumnaa Added the erep api class.
authored
234 $this->debug_string .= '<b>Request sent:</b> ' . htmlentities($this->request_url);
235 if ($this->request_method == "POST") {
236 $this->debug_string .= ' - ' . $parameters . ' - ' . (count($this->params) + 1);
237 }
a82159d @Kumnaa Erep api class tidy up.
authored
238
47d84a8 @Kumnaa Added the erep api class.
authored
239 $this->debug_string .= '<br /><br />';
240 $ch = curl_init();
241 curl_setopt($ch, CURLOPT_URL, $this->request_url);
242 if ($this->request_method == "POST") {
243 curl_setopt($ch, CURLOPT_POST, count($this->params) + 1);
244 curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
245 }
a82159d @Kumnaa Erep api class tidy up.
authored
246
47d84a8 @Kumnaa Added the erep api class.
authored
247 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
248 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->api_timeout);
249 $data = curl_exec($ch);
a82159d @Kumnaa Erep api class tidy up.
authored
250 if ($data == false) {
47d84a8 @Kumnaa Added the erep api class.
authored
251 throw new Exception(curl_error($ch));
252 }
a82159d @Kumnaa Erep api class tidy up.
authored
253
47d84a8 @Kumnaa Added the erep api class.
authored
254 curl_close($ch);
255 $this->debug_string .= '<b>Data from api:</b> ' . htmlentities($data) . '<br /><br />';
256 return ($data);
257 }
258
259 // generate signature
260 private function gen_signature() {
261 $raw_sig = hash_hmac('sha1', $this->gen_base_string(), $this->secret, true);
262 $b64_sig = base64_encode($raw_sig);
263 $this->debug_string .= '<b>Signature:</b> ' . $b64_sig . '<br /><br />';
264 return ($b64_sig);
265 }
266
267 // generate base string
268 private function gen_base_string() {
269 ksort($this->params);
270 foreach ($this->params as $k => $v) {
271 if (($k == 'oauth_token' && strlen($v) > 0) || $k != 'oauth_token') {
272 $enc_params[] = urlencode($k) . '=' . urlencode($v);
273 }
274 }
a82159d @Kumnaa Erep api class tidy up.
authored
275
47d84a8 @Kumnaa Added the erep api class.
authored
276 $this->normalised_string = implode('&', $enc_params);
277 $this->debug_string .= '<b>Normalised String:</b> ' . $this->normalised_string . '<br /><br />';
278 $return = $this->request_method . '&' . urlencode($this->request_url) . '&' . urlencode($this->normalised_string);
279 $this->debug_string .= '<b>Base String:</b> ' . $return . '<br /><br />';
280 return ($return);
281 }
282
283 // generate secret
284 private function gen_secret($token_secret = '') {
285 $this->secret = urlencode($this->consumer_secret) . "&" . urlencode($token_secret);
286 }
287
288 // generate the request url
289 private function gen_request_url($action) {
290 $this->request_url = $this->url_domain . '/' . $this->subject . '/' . $action;
291 }
292
293 public function debug() {
294 return $this->debug_string;
295 }
296
297 private function handle_xml($data) {
298 $xml = @simplexml_load_string($data);
299 if ($xml != false) {
300 if (isset($xml->head) && isset($xml->body)) {
301 throw new Exception("An html error was encountered: " . nl2br(htmlentities($xml->body)));
302 } elseif (isset($xml->message)) {
303 throw new Exception("An oauth error was encountered: " . $xml->message);
304 } else {
305 $data = $xml;
306 }
307 } else {
308 throw new Exception("Invalid xml returned: " . $data);
309 }
a82159d @Kumnaa Erep api class tidy up.
authored
310
47d84a8 @Kumnaa Added the erep api class.
authored
311 return $data;
312 }
313
314 }
315
316 ?>
Something went wrong with that request. Please try again.