ClashOfClans::sendRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
require_once "League.class.php";
3
require_once "Location.class.php";
4
require_once "Clan.class.php";
5
require_once "Member.class.php";
6
require_once "Warlog.class.php";
7
require_once "LogEntry.class.php";
8
require_once "Player.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_SSL_VERIFYPEER, false);
30
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
31
  			'authorization: Bearer '.$this->_apiKey // 
32
		));
33
		$output = curl_exec($ch);
34
		curl_close($ch); 
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
	 * Get Player.
79
	 */
80
	public function getPlayer($tag)
81
	{
82
		$json = $this->sendRequest("https://api.clashofclans.com/v1/players/" . urlencode($tag));
83
		return json_decode($json);
84
	}
85
86
	/**
87
	 * Get information of a clan
88
	 *
89
	 * @param $tag, clantag. (e.g. #22UCCU0J)
90
	 * @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...
91
	 */
92
	public function getClanByTag($tag) //#22UCCU0J = foxforcefürth
93
	{
94
		$json = $this->sendRequest("https://api.clashofclans.com/v1/clans/".urlencode($tag));
95
		return json_decode($json);
96
	}
97
98
	/**
99
	 * Get information about the membersof a clan
100
	 *
101
	 * @param $tag, clantag. (e.g. #22UCCU0J)
102
	 * @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...
103
	 */
104
	public function getClanMembersByTag($tag)
105
	{
106
		$json = $this->sendRequest("https://api.clashofclans.com/v1/clans/".urlencode($tag)."/members");
107
		return json_decode($json);
108
	}
109
110
	/**
111
	 * Get a list of all locations supported by SuperCell's Clan-System
112
	 *
113
	 * @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...
114
	 */
115
	public function getLocationList()
116
	{
117
		$json = $this->sendRequest("https://api.clashofclans.com/v1/locations");
118
		return json_decode($json);
119
	}
120
121
	/**
122
	 * Get information about a location by providing it's id.
123
	 *
124
	 * @param $locationId
125
	 * @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...
126
	 */
127
	public function getLocationInfo($locationId) //32000094 = Germany
128
	{
129
		$json = $this->sendRequest("https://api.clashofclans.com/v1/locations/".$locationId);
130
		return json_decode($json);
131
	}
132
133
	/**
134
	 * Get information about all leages.
135
	 *
136
	 * @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...
137
	 */
138
	public function getLeagueList()
139
	{
140
		$json = $this->sendRequest("https://api.clashofclans.com/v1/leagues");
141
		return json_decode($json);
142
	}
143
144
	/**
145
	 * Get ranklist information about players or clans
146
	 *
147
	 * @param $locationId (tip: 32000006 is "International")
148
	 * @param (optional) $clans
149
	 * @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...
150
	 */
151
	public function getRankList($locationId, $clans = false) //if clans is not set to true, return player ranklist
152
	{
153
		if ($clans)
154
		{
155
			$json = $this->sendRequest("https://api.clashofclans.com/v1/locations/".$locationId."/rankings/clans");
156
		}
157
		else
158
		{
159
			$json = $this->sendRequest("https://api.clashofclans.com/v1/locations/".$locationId."/rankings/players");
160
		}
161
		return json_decode($json);
162
	}
163
	
164
	/**
165
	 * Get whether the war log of a specific clan is public or not 
166
	 *
167
	 * @param $tag, clan tag
168
	 * @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...
169
	 */
170
	public function isWarlogPublic($tag)
171
	{
172
		$json = $this->sendRequest("https://api.clashofclans.com/v1/clans/".urlencode($tag)."/warlog");
173
		$logInfo = json_decode($json);
174
		if(property_exists($logInfo, "reason"))
175
		{
176
			if($logInfo->reason == "accessDenied")
177
			{
178
				return false;
179
			}
180
		}
181
		return true;
182
	}
183
	
184
	/**
185
	 * Get a clan's warlog by specifying it's clantag
186
	 *
187
	 * @param $tag, clan tag
188
	 * @param (optional) $parameters array, other parameters (before, after, limit)
189
	 * @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...
190
	 */
191
	public function getWarlog($tag, $parameters = "")
192
	{
193
		if($this->isWarlogPublic($tag))
194
		{
195
			$json = $this->sendRequest("https://api.clashofclans.com/v1/clans/".urlencode($tag)."/warlog?".http_build_query($parameters));
196
			return json_decode($json);
197
		}
198
		else
199
		{
200
			//dummy warlog:
201
			return json_decode('{
202
			  "items": [
203
				{
204
				  "result": "win",
205
				  "endTime": "20160803T103621.000Z",
206
				  "teamSize": 0,
207
				  "clan": {
208
					"tag": "#22UCCU0J",
209
					"name": "foxforce",
210
					"badgeUrls": {
211
					  "small": "https://api-assets.clashofclans.com/badges/70/5tEhjxjRbdef_mG_PGehpowUmLJU4qPnY7zQJPv1Lj0.png",
212
					  "large": "https://api-assets.clashofclans.com/badges/512/5tEhjxjRbdef_mG_PGehpowUmLJU4qPnY7zQJPv1Lj0.png",
213
					  "medium": "https://api-assets.clashofclans.com/badges/200/5tEhjxjRbdef_mG_PGehpowUmLJU4qPnY7zQJPv1Lj0.png"
214
					},
215
					"clanLevel": 0,
216
					"attacks": 0,
217
					"stars": 0,
218
					"destructionPercentage": 0,
219
					"expEarned": 0
220
				  },
221
				  "opponent": {
222
					"tag": "#22UCCU0J",
223
					"name": "foxforce",
224
					"badgeUrls": {
225
					  "small": "https://api-assets.clashofclans.com/badges/70/LGoHdPrA6OiVcKYzDcIiF7SV8kWW1qd-EhWkvGPsARM.png",
226
					  "large": "https://api-assets.clashofclans.com/badges/512/LGoHdPrA6OiVcKYzDcIiF7SV8kWW1qd-EhWkvGPsARM.png",
227
					  "medium": "https://api-assets.clashofclans.com/badges/200/LGoHdPrA6OiVcKYzDcIiF7SV8kWW1qd-EhWkvGPsARM.png"
228
					},
229
					"clanLevel": 0,
230
					"stars": 0,
231
					"destructionPercentage": 0
232
				  }
233
				}
234
			  ],
235
			  "paging": {
236
				"cursors": {
237
				  "after": "0"
238
				}
239
			  }
240
			}');
241
		}
242
	}
243
};
244
245
?>
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...
246