Skip to main content

Clash of Clans Api – Basics Part 2 | Api Tutorial

Before you read this post I will strongly suggest that you go through CLASH OF CLANS API tutorial Part 1. if you haven’t read it yet. In this post we will see how to use TOKEN generated in part-1 to show Clash of Clans API data into a page in WordPress.

Clash of Clans Api Basics part 2

We will fetch Clash of Clans league data via api and will display it in a wordpress page. Let’s check how the Leagues Data via API looks like:

Clash of clans api documation

Login into https://developer.clashofclans.com called Clash of Clans API development portal.

Click on Documentation link at top now.

Now select Leagues option.

Scroll down and Click on “Try it out!” button. You can see the data in json format.The League API data consists of primarily 3 key-value pairs namely:
id: It is unique number assigned to each League in Clash of Clans. This id will help in identifying League properly.
name: It is name assigned to each League in Clash of Clans.
iconUrls: It is absolute path to the League images. There are 3 image sizes available –> small, tiny & medium.
Example Input:
In this example we will fetch all the available Leagues in Clash of Clans and will display in a tabular format.
Example Output:
In wordpress page you cannot directly write PHP Code. So we will create a template and this template will be used in WordPress Page. Let’s start with creating a wordpress template to show all Leagues information.

Requirements For Coc Api Tutorial

1) Already installed WordPress
2) Token corresponding to the IP (Coc API – Basics- Part 1)
3) API URL which in this case is https://api.clashofclans.com/v1/leagues

Open your wordpress installation folder and go to /wp-content/themes/YOUR_THEME/ and transfer page.php file into local disk.
Make a copy of page.php file. You can use any file transfer software to copy files into your local disk. FileZilla and WinSCP are two most popular FTP clients for windows.

Clash of Clans Api template


Edit the Template Name mentioned at the start.
<?php
/*
Template Name: COC_API_League
*/
?>

Keep the header, sidebar and footer section as it is
<?php get_header(); ?>
<?php get_footer(); ?>
<?php get_sidebar(); ?>

In the content section add following PHP code:
<?php

$api_key = ‘YOUR_API_KEY‘;
$url = ‘https://api.clashofclans.com/v1/leagues’;
$var_cntr = 0;
$var_s_no = 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);

echo “<table>”;
echo “<tr><th>S.No</th><th>League ID</th><th>League Name</th><th>League Badge</th></tr>”;
while (!empty($response[‘items’][$var_cntr][‘id’]))
{
echo “<tr>”;
echo “<td>”.$var_s_no.”</td><td>”.$response[‘items’][$var_cntr][‘id’].”</td><td>”.$response[‘items’][$var_cntr][‘name’].”</td>
<td><img src='”.$response[‘items’][$var_cntr][‘iconUrls’][‘small’].”‘ />”;
echo “</tr>”;
$var_cntr++;
$var_s_no++;
}
echo “</table>”;
curl_close($ch);
?>

We have used PHP CURL library to read API data. learn more about curl and available functions. The logic is simple. We will fetch API data into a variable. You may consider API JSON data as one big String value. Next step is to run the loop from index 0 till we continue to get the record and display it on the webpage.

Conclusion

If you don’t have much interest in programming then you need not go into details. Just copy the above code and paste it in a php file. Once done save the file as API_Leagues.php and transfer the file to WordPress theme folder. Now login into WordPress admin panel. Create a new page and give it some name. On the right side in “Page Attribute” Section click on “Template” drop down and select “COC_API_League“. Now publish the page. Browse to the page link and view your new page. You should be able to see a table with all the Leagues information via API.

In the next post we will see how to save the API data into MYSQL database.

Popular posts from this blog

Drag attack strategy for th8 ultimate guide

Clash of clans community discovered thousands of interesting and effective attack strategies including drag attack strategy. Drag attack normally starts at th7 and lasts till th9(I saw to use them even in th10).
The combination of each townhall would be different. Here we are experimenting with a th8 base. It's not to say it's as popular as effective in th8.
Troops for a successful drag attack
Dragons-10/9(Player differ)Balloon-0/4 Max or lvl 6 loons in castleLightning spell-2Rage spell-1Eq spell-1Poison/haste-1 in castle
If you are taking 9 drags. Make sure to take 4 loons. How many drags you have to choose depend on the base you're going to attack.First Step:
You don't have to clear clan castle troops in drag attack. However be careful to drop the poison. Anyhow drop 2 lightning spell and 1 eq spell on any air defence and it will destroy the air defence.
Second Step:
Now release all drags on a line from the closest side of air defence. Once they are ready to taking da…

Th9 witch bowler combo attack strategy

This attack strategy is basically useful in the bases with centralised air defences. If the air defence is in outside compartments then the strategy might not as effective with is attacking strategy.
Begain with witch with bowler Here the strategy goes in such way : Golem, Bowlers and Heroes along with wizards are the kill squad. These troops will break major defences like x-bows and air defence with few other structures and make the base easy for the the witches who are healed continuously by the healer. Minimum troops & spells levels requirements to get success in this th9 attack strategyWitch lev 2Jump spell lev 2Golem lev 3Poison spell lev 2Wizard lev 6Healing spell lev 5Rage spell lev 5Archer lev 5Barbarian King and Archer Queen must be more than lev 12 In this th9 war attack strategy player need troops combo of
10 Witch1 Golem3 Wizard4 Healer2 Archer And spells combo of
2 Jump spells-1Healing spell-1Rage spell1 PoisonIn Clan castle- 6 Bowlers(Max level) and 1 Poison spell

Clash of Clans API – Basics Part 1 | API Tutorial

I am sure you love Clash of Clans because you are reading this post and wish to know more about COC API. Few years back Supercell launched Clash of Clans API for public use. Clash of Clans API gives so much information about Clans and Clan Wars however in the next version we can expect individual clasher details too. After which Clash of Clans API will become truly helpful in getting insight into Clan data.
Clash of Clans API tutorial part 1 Target Audience Non-technical Clash of Clans clashers who want to use API to get insight into their Clan using available historical data.
Technology Used in APIWordPress as platform
PHP as programming language
MySQL as Database What is Clash of Clans API? COC API can be considered as a medium to access clash of clans data in secure manner.
What all information is available in Clash of Clans API? In VERSION v1, following information is available:

Clans: Search for Clans using Clan Tag, Name, Clan Level, Clan Points, Clan Members etc. You can also sea…