1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Maniaplanet\WebServices;
14:
15: 16: 17: 18:
19: abstract class HTTPClient
20: {
21: const VERSION = '3.0.6';
22:
23: private static $HTTPStatusCodes = array(
24: 100 => 'Continue',
25: 200 => 'Sucess',
26: 201 => 'Created',
27: 202 => 'Accepted',
28: 203 => 'Non-Authoritative Information',
29: 204 => 'No Content',
30: 205 => 'Reset Content',
31: 206 => 'Partial Content',
32: 300 => 'Multiple Choices',
33: 301 => 'Moved Permanently',
34: 302 => 'Found',
35: 303 => 'See Other',
36: 304 => 'Not Modified',
37: 305 => 'Use Proxy',
38: 307 => 'Temporary Redirect',
39: 400 => 'Bad Request',
40: 401 => 'Unauthorized',
41: 402 => 'Payment Required',
42: 403 => 'Forbidden',
43: 404 => 'Not Found',
44: 405 => 'Method Not Allowed',
45: 406 => 'Not Acceptable',
46: 409 => 'Conflict',
47: 410 => 'Gone',
48: 411 => 'Length Required',
49: 412 => 'Precondition Failed',
50: 413 => 'Request Entity Too Large',
51: 414 => 'Request-URI Too Long',
52: 415 => 'Unsupported Media Type',
53: 416 => 'Requested Range Not Satisfiable',
54: 417 => 'Expectation Failed',
55: 500 => 'Internal Server Error',
56: 501 => 'Not Implemented',
57: 503 => 'Service Unavailable',
58: );
59:
60: 61: 62: 63: 64:
65: protected $APIURL = 'https://ws.maniaplanet.com';
66:
67: 68: 69: 70: 71:
72: protected $username;
73:
74: 75: 76: 77: 78:
79: protected $password;
80:
81: 82: 83: 84: 85:
86: protected $enableAuth = true;
87:
88: 89: 90: 91: 92:
93: protected $throwExceptions = true;
94:
95: 96: 97: 98: 99:
100: public $lastException;
101:
102: 103: 104: 105: 106:
107: protected $contentType = 'application/json';
108:
109: 110: 111: 112: 113:
114: protected $accept = 'application/json';
115:
116: 117: 118: 119: 120:
121: protected $serializeCallback = 'json_encode';
122:
123: 124: 125:
126: protected $unserializeCallback = 'json_decode';
127:
128: 129: 130: 131: 132:
133: protected = array();
134:
135: 136: 137: 138: 139: 140: 141: 142: 143:
144: function __construct($username = null, $password = null)
145: {
146: if(!function_exists('curl_init'))
147: {
148: trigger_error('You must activate the CURL PHP extension.', E_USER_ERROR);
149: }
150: if(!function_exists('json_encode'))
151: {
152: trigger_error('You must activate the JSON PHP extension.', E_USER_ERROR);
153: }
154:
155:
156: if(!$username && !$password && class_exists('\ManiaLib\WebServices\Config'))
157: {
158: $config = \ManiaLib\WebServices\Config::getInstance();
159: $username = $config->username;
160: $password = $config->password;
161: }
162:
163: $this->username = $username;
164: $this->password = $password;
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: protected function execute($method, $ressource, array $params = array())
193: {
194: $this->lastException = null;
195:
196: $url = $this->APIURL.$ressource;
197:
198:
199:
200: if($method == 'POST' || $method == 'PUT')
201: {
202: $data = array_pop($params);
203: if($this->serializeCallback)
204: {
205: $data = call_user_func($this->serializeCallback, $data);
206: }
207: }
208: else
209: {
210: $data = null;
211: }
212:
213:
214:
215: if($params)
216: {
217: $params = array_map('urlencode', $params);
218: array_unshift($params, $url);
219: $url = call_user_func_array('sprintf', $params);
220: }
221:
222:
223: $options = array();
224: $header = array();
225:
226: $header[] = 'Accept: '.$this->accept;
227: $header[] = 'Content-type: '.$this->contentType;
228: $header = array_merge($header, $this->headers);
229:
230: switch($method)
231: {
232: case 'GET':
233:
234: break;
235:
236: case 'POST':
237: $options[CURLOPT_POST] = true;
238: $options[CURLOPT_POSTFIELDS] = $data;
239: break;
240:
241: case 'PUT':
242: $fh = fopen('php://temp', 'rw');
243: fwrite($fh, $data);
244: rewind($fh);
245: $options[CURLOPT_PUT] = true;
246: $options[CURLOPT_INFILE] = $fh;
247: $options[CURLOPT_INFILESIZE] = strlen($data);
248: break;
249:
250: case 'DELETE':
251: $options[CURLOPT_POST] = true;
252: $options[CURLOPT_POSTFIELDS] = '';
253: $options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
254: break;
255:
256: default:
257: throw new \InvalidArgumentException('Unsupported HTTP method: '.$method);
258: }
259:
260: $options[CURLOPT_URL] = $url;
261: $options[CURLOPT_HTTPHEADER] = $header;
262: if($this->enableAuth && $this->username)
263: {
264: $options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
265: $options[CURLOPT_USERPWD] = $this->username.':'.$this->password;
266: }
267: $options[CURLOPT_TIMEOUT] = 10;
268: $options[CURLOPT_RETURNTRANSFER] = true;
269: $options[CURLOPT_USERAGENT] = 'maniaplanet-ws-sdk/'.self::VERSION;
270:
271: $options[CURLOPT_SSL_VERIFYHOST] = 0;
272: $options[CURLOPT_SSL_VERIFYPEER] = 0;
273:
274: try
275: {
276: $ch = curl_init();
277: curl_setopt_array($ch, $options);
278: $responseBody = curl_exec($ch);
279: $responseBodyRaw = $responseBody;
280: $responseInfo = curl_getinfo($ch);
281: curl_close($ch);
282: }
283: catch(\Exception $e)
284: {
285: if($ch)
286: {
287: curl_close($ch);
288: }
289: throw $e;
290: }
291:
292: if($responseInfo['http_code'] == 200)
293: {
294: if($responseBody)
295: {
296: if($this->unserializeCallback)
297: {
298: $responseBody = call_user_func($this->unserializeCallback, $responseBody);
299: }
300: }
301: return $responseBody;
302: }
303: else
304: {
305: $message = '';
306: $code = 0;
307: $statusCode = $responseInfo['http_code'];
308: $statusMessage = null;
309:
310: if(array_key_exists($statusCode, self::$HTTPStatusCodes))
311: {
312: $statusMessage = self::$HTTPStatusCodes[$statusCode];
313: }
314:
315: if($responseBody)
316: {
317: if($this->unserializeCallback)
318: {
319: $responseBody = call_user_func($this->unserializeCallback, $responseBody);
320: }
321: if(is_object($responseBody))
322: {
323: if(property_exists($responseBody, 'message'))
324: {
325: $message = $responseBody->message;
326: }
327: elseif(property_exists($responseBody, 'error'))
328: {
329: $message = $responseBody->error;
330: }
331: if(property_exists($responseBody, 'code'))
332: {
333: $code = $responseBody->message;
334: }
335: }
336: }
337:
338: $exception = new Exception($message, $code, $statusCode, $statusMessage);
339:
340: if($this->throwExceptions)
341: {
342: throw $exception;
343: }
344: else
345: {
346: $this->lastException = $exception;
347: return false;
348: }
349: }
350: }
351:
352: }
353:
354: ?>