Passed
Push — master ( d1c1a6...f81abb )
by Nico
02:34
created

ClashOfClans::sendRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
require_once "League.class.php";
4
require_once "Location.class.php";
5
require_once "Clan.class.php";
6
require_once "Member.class.php";
7
require_once "Warlog.class.php";
8
require_once "LogEntry.class.php";
9
10
/**
11
 * Class to get JSON-decoded arrays containing information provided by SuperCell's official Clash of Clans API located at https://developer.clashofclans.com
12
 */
13
14
class ClashOfClans
15
{
16
	private $_apiKey = "Change_This_To_Your_Token";
17
	
18
	/**
19
	 * Send a Request to SuperCell's Servers and contains the authorization-Token.
20
	 *
21
	 * @param string $url
22
	 * @return string; response from API (json)
0 ignored issues
show
Documentation introduced by
The doc-type string; could not be parsed: Expected "|" or "end of type", but got ";" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
23
	 */
24
	protected function sendRequest($url)
25
	{
26
		$ch = curl_init(); 
27
		curl_setopt($ch, CURLOPT_URL, $url);
28
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
29
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
30
  			'authorization: Bearer '.$this->_apiKey // 
31
		));
32
		$output = curl_exec($ch);
33
		curl_close($ch); 
34
35
		return $output;
36
	}
37
38
	/**
39
	 * Search all clans by name
40
	 *
41
	 * @param $searchString, the clan name, e.g. foxforcefürth
42
	 * @return object, search results.
0 ignored issues
show
Documentation introduced by
The doc-type object, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
43
	 */
44
	public function searchClanByName($searchString)
45
	{
46
		$json = $this->sendRequest("https://api.clashofclans.com/v1/clans?name=".urlencode($searchString));
47
		return json_decode($json);
48
	}
49
50
	/** 
51
	 * Search for clans by using multiple parameters
52
	 * 
53
	 * @param array
54
	 * @return object
55
	 */
56
	public function searchClan($parameters)
57
	{
58
		/*
59
		Array can have these indexes: 
60
		* name (string)
61
		* warFrequency (string, {"always", "moreThanOncePerWeek", "oncePerWeek", "lessThanOncePerWeek", "never", "unknown"})
62
		* locationId (integer)
63
		* minMembers (integer)
64
		* maxMembers (integer)
65
		* minClanPoints (integer)
66
		* minClanLevel (integer)
67
		* limit (integer)
68
		* after (integer)
69
		* before (integer)
70
		For more information, take a look at the official documentation: https://developer.clashofclans.com/#/documentation
71
		*/
72
73
		$json = $this->sendRequest("https://api.clashofclans.com/v1/clans?".http_build_query($parameters));
74
		return json_decode($json);
75
	}
76
77
78
	/**
79
	 * Get information of a clan
80
	 *
81
	 * @param $tag, clantag. (e.g. #22UCCU0J)
82
	 * @return object, clan information.
0 ignored issues
show
Documentation introduced by
The doc-type object, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
83
	 */
84
	public function getClanByTag($tag) //#22UCCU0J = foxforcefürth
85
	{
86
		$json = $this->sendRequest("https://api.clashofclans.com/v1/clans/".urlencode($tag));
87
		return json_decode($json);
88
	}
89
90
	/**
91
	 * Get information about the membersof a clan
92
	 *
93
	 * @param $tag, clantag. (e.g. #22UCCU0J)
94
	 * @return object, member information.
0 ignored issues
show
Documentation introduced by
The doc-type object, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
95
	 */
96
	public function getClanMembersByTag($tag)
97
	{
98
		$json = $this->sendRequest("https://api.clashofclans.com/v1/clans/".urlencode($tag)."/members");
99
		return json_decode($json);
100
	}
101
102
	/**
103
	 * Get a list of all locations supported by SuperCell's Clan-System
104
	 *
105
	 * @return object, all locations.
0 ignored issues
show
Documentation introduced by
The doc-type object, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
106
	 */
107
	public function getLocationList()
108
	{
109
		$json = $this->sendRequest("https://api.clashofclans.com/v1/locations");
110
		return json_decode($json);
111
	}
112
113
	/**
114
	 * Get information about a location by providing it's id.
115
	 *
116
	 * @param $locationId
117
	 * @return object, location info.
0 ignored issues
show
Documentation introduced by
The doc-type object, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
118
	 */
119
	public function getLocationInfo($locationId) //32000094 = Germany
120
	{
121
		$json = $this->sendRequest("https://api.clashofclans.com/v1/locations/".$locationId);
122
		return json_decode($json);
123
	}
124
125
	/**
126
	 * Get information about all leages.
127
	 *
128
	 * @return object, league info.
0 ignored issues
show
Documentation introduced by
The doc-type object, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
129
	 */
130
	public function getLeagueList()
131
	{
132
		$json = $this->sendRequest("https://api.clashofclans.com/v1/leagues");
133
		return json_decode($json);
134
	}
135
136
	/**
137
	 * Get ranklist information about players or clans
138
	 *
139
	 * @param $locationId (tip: 32000006 is "International")
140
	 * @param (optional) $clans
141
	 * @return object, location info.
0 ignored issues
show
Documentation introduced by
The doc-type object, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
142
	 */
143
	public function getRankList($locationId, $clans = false) //if clans is not set to true, return player ranklist
144
	{
145
		if ($clans)
146
		{
147
			$json = $this->sendRequest("https://api.clashofclans.com/v1/locations/".$locationId."/rankings/clans");
148
		}
149
		else
150
		{
151
			$json = $this->sendRequest("https://api.clashofclans.com/v1/locations/".$locationId."/rankings/players");
152
		}
153
		return json_decode($json);
154
	}
155
	
156
	/**
157
	 * Get whether the war log of a specific clan is public or not 
158
	 *
159
	 * @param $tag, clan tag
160
	 * @return bool, warlog public yes/no.
0 ignored issues
show
Documentation introduced by
The doc-type bool, could not be parsed: Expected "|" or "end of type", but got "," at position 4. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
161
	 */
162
	public function isWarlogPublic($tag)
163
	{
164
		$json = $this->sendRequest("https://api.clashofclans.com/v1/clans/".urlencode($tag)."/warlog");
165
		$logInfo = json_decode($json);
166
		if(property_exists($logInfo, "reason"))
167
		{
168
			if($logInfo->reason == "accessDenied")
169
			{
170
				return false;
171
			}
172
		}
173
		return true;
174
	}
175
	
176
	/**
177
	 * Get a clan's warlog by specifying it's clantag
178
	 *
179
	 * @param $tag, clan tag
180
	 * @param (optional) $parameters array, other parameters (before, after, limit)
181
	 * @return object, warlog. Dummy warlog when warlog not public.
0 ignored issues
show
Documentation introduced by
The doc-type object, could not be parsed: Expected "|" or "end of type", but got "," at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
182
	 */
183
	public function getWarlog($tag, $parameters = "")
184
	{
185
		if($this->isWarlogPublic($tag))
186
		{
187
			$json = $this->sendRequest("https://api.clashofclans.com/v1/clans/".urlencode($tag)."/warlog?".http_build_query($parameters));
188
			return json_decode($json);
189
		}
190
		else
191
		{
192
			//dummy warlog:
193
			return json_decode('{
194
			  "items": [
195
				{
196
				  "result": "win",
197
				  "endTime": "20160803T103621.000Z",
198
				  "teamSize": 0,
199
				  "clan": {
200
					"tag": "#22UCCU0J",
201
					"name": "foxforce",
202
					"badgeUrls": {
203
					  "small": "https://api-assets.clashofclans.com/badges/70/5tEhjxjRbdef_mG_PGehpowUmLJU4qPnY7zQJPv1Lj0.png",
204
					  "large": "https://api-assets.clashofclans.com/badges/512/5tEhjxjRbdef_mG_PGehpowUmLJU4qPnY7zQJPv1Lj0.png",
205
					  "medium": "https://api-assets.clashofclans.com/badges/200/5tEhjxjRbdef_mG_PGehpowUmLJU4qPnY7zQJPv1Lj0.png"
206
					},
207
					"clanLevel": 0,
208
					"attacks": 0,
209
					"stars": 0,
210
					"destructionPercentage": 0,
211
					"expEarned": 0
212
				  },
213
				  "opponent": {
214
					"tag": "#22UCCU0J",
215
					"name": "foxforce",
216
					"badgeUrls": {
217
					  "small": "https://api-assets.clashofclans.com/badges/70/LGoHdPrA6OiVcKYzDcIiF7SV8kWW1qd-EhWkvGPsARM.png",
218
					  "large": "https://api-assets.clashofclans.com/badges/512/LGoHdPrA6OiVcKYzDcIiF7SV8kWW1qd-EhWkvGPsARM.png",
219
					  "medium": "https://api-assets.clashofclans.com/badges/200/LGoHdPrA6OiVcKYzDcIiF7SV8kWW1qd-EhWkvGPsARM.png"
220
					},
221
					"clanLevel": 0,
222
					"stars": 0,
223
					"destructionPercentage": 0
224
				  }
225
				}
226
			  ],
227
			  "paging": {
228
				"cursors": {
229
				  "after": "0"
230
				}
231
			  }
232
			}');
233
		}
234
	}
235
};
236
237
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...