-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAPI.php
More file actions
99 lines (89 loc) · 3.3 KB
/
API.php
File metadata and controls
99 lines (89 loc) · 3.3 KB
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
<?php
require_once(dirname(__FILE__) . '/Tournament.php');
require_once(dirname(__FILE__) . '/Participant.php');
require_once(dirname(__FILE__) . '/Match.php');
define('CHALLONGE_RESPONSE_OK', '200');
define('CHALLONGE_RESPONSE_INVALID_KEY', '401');
define('CHALLONGE_RESPONSE_NOT_FOUND', '404');
define('CHALLONGE_RESPONSE_VALIDATION_ERROR', '422');
class ChallongeAPI
{
const API_URL = 'https://api.challonge.com/v1/';
const API_EXT = '.xml';
static public $api_key;
protected $params = array();
public function setParam($name, $value)
{
$this->params[$name] = $value;
}
public function getParam($name, $default = null)
{
return array_key_exists($name, $this->params) ? $this->params[$name] : $default;
}
public function setParams($params)
{
foreach ($params as $name => $value)
{
$this->setParam($name, $value);
}
}
public function request($url_append = '', $method = 'get', $params = array())
{
$params = array_merge($this->params, $params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
switch($method)
{
case 'get':
curl_setopt($ch, CURLOPT_URL, $this->prepareURL($url_append, $params));
break;
case 'post':
curl_setopt($ch, CURLOPT_URL, $this->prepareURL($url_append));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, '', '&'));
break;
case 'put':
$params['_method'] = 'put';
curl_setopt($ch, CURLOPT_URL, $this->prepareURL($url_append));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, '', '&'));
break;
case 'delete':
$params['_method'] = 'delete';
curl_setopt($ch, CURLOPT_URL, $this->prepareURL($url_append));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, '', '&'));
break;
default:
return false;
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
if ($response === false)
{
$response = '<local><error>' . curl_error($ch) . '</error></local>';
}
curl_close($ch);
return $this->handleResponse($response);
}
public function prepareURL($url_append, $params = array())
{
return self::API_URL . $url_append . self::API_EXT . '?' . http_build_query(array_merge($params, array('api_key' => self::$api_key)), '', '&');
}
protected function handleResponse($response)
{
libxml_use_internal_errors(true);
try{
$return_object = new SimpleXMLElement($response);
//For some html responses, an exception isn't thrown.
if(!$return_object){
return simplexml_load_string('<local><error> Unable to parse XML </error></local>');
}
}
catch(Exception $e)
{
return simplexml_load_string('<local><error> Unable to parse XML </error></local>');
}
return $return_object;
}
}