Today we will learn how to accept CLAN NAME as input from user and show CLAN Details on a webpage. Before we proceed you should know How to generate access key for Clash of Clans API and How to fetch and display API data on a web page. If you have not read previous posts I will strongly recommend you to take 5 mins and go through those posts. Alright lets build a new page which will accept Clan Name from user and will display relevant information.
Step 1: Build a new page with the same template as we have used for Leagues API demo (can be found at last of this post). Just Copy that page and do some modifications in the code. First thing we need a Search Form in which a user can mention CLAN Name and other details. To keep it simple we will just search for clan name and list down all the clans with that name. Create a page in WordPress and give it any name say “ClanSearch”. Now in your template page add below code:
Clan Search:<input type=”text” id=”clanname” name=”clanname” placeholder=”Enter clan name” />
<input type=”submit” value=”Search”>
</form>
How to show a clan search form in a webpage
Step 1: Build a new page with the same template as we have used for Leagues API demo (can be found at last of this post). Just Copy that page and do some modifications in the code. First thing we need a Search Form in which a user can mention CLAN Name and other details. To keep it simple we will just search for clan name and list down all the clans with that name. Create a page in WordPress and give it any name say “ClanSearch”. Now in your template page add below code:
Clan Search:<input type=”text” id=”clanname” name=”clanname” placeholder=”Enter clan name” />
<input type=”submit” value=”Search”>
</form>
Step 2: Once you have created a form to read clan name enter below code to check if user submitted any clan name. If not we will use a default clan name (RETURNOFTHEFALL is my clan).
if(isset($_POST[‘clanname’]) && (!(empty($_POST[‘clanname’]))) ) { /*start – if clan name is entered */
$var_clanname= strtoupper($_POST[‘clanname’]) ;
}
else
{
$var_clanname=’returnofthefall’;
}
Step 3: Fetch API Data for the clan name submitted by user.
$api_key = ‘YOUR_API_KEY_HERE’;
$url = ‘https://api.clashofclans.com/v1/clans?name=’.$var_clanname;
$var_cntr = 0;
$var_cntr1 = 1;
$headers = array(
“Accept: application/json”,
“Authorization: Bearer ” . $api_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
$response = json_decode($result,true);
Step 4: Show data on a webpage now.
echo “<center><table>”;
echo “<tr><th>S.No</th><th>Badge</th><th>Clan Tag</th><th>Clan Name</th><th>Location</th><th>Clan Level</th><th>War Wins</th></tr>”;
while (!empty($response[‘items’][$var_cntr][‘tag’]))
{
echo “<tr>”;
echo “<td>”.$var_cntr1.”</td><td><img src=”.$response[‘items’][$var_cntr][‘badgeUrls’][‘small’].” /></td><td>”.$response[‘items’][$var_cntr][‘tag’].”</td><td>”.$response[‘items’][$var_cntr][‘name’].”</td>
<td>”.$response[‘items’][$var_cntr][‘location’][‘name’].”</td><td>”.$response[‘items’][$var_cntr][‘clanLevel’].”</td>
<td>”.$response[‘items’][$var_cntr][‘warWins’].”</td>”;
echo “</tr>”;
$var_cntr++;
$var_cntr1++;
}
echo “</table></center>”;
League API Details
S.No | League ID | League Name | League Badge |
---|---|---|---|
1 | 29000000 | Unranked | |
2 | 29000001 | Bronze League III | |
3 | 29000002 | Bronze League II | |
4 | 29000003 | Bronze League I | |
5 | 29000004 | Silver League III | |
6 | 29000005 | Silver League II | |
7 | 29000006 | Silver League I | |
8 | 29000007 | Gold League III | |
9 | 29000008 | Gold League II | |
10 | 29000009 | Gold League I | |
11 | 29000010 | Crystal League III | |
12 | 29000011 | Crystal League II | |
13 | 29000012 | Crystal League I | |
14 | 29000013 | Master League III | |
15 | 29000014 | Master League II | |
16 | 29000015 | Master League I | |
17 | 29000016 | Champion League III | |
18 | 29000017 | Champion League II | |
19 | 29000018 | Champion League I | |
20 | 29000019 | Titan League III | |
21 | 29000020 | Titan League II | |
22 | 29000021 | Titan League I | |
23 | 29000022 | Legend League |