Skip to main content

Clash of Clans API – Basics- Part 3 | Api Tutorial

In the last 2 posts we read about "How to generate access key for Clash of Clans API" and "How to fetch and display API data on a web page". In this post we will see how to save the data into a MYSQL table for persistent storage. First step is to create a Table in MYSQL database. Since in our example we are using WordPress, we will create new table in WordPress database itself. If you wish you can make another database and also new user for better security purpose.

Clash of clans Api Basics Part-3

How to save the data into a MYSQL table for persistent storage.

Step 1: Login into cpanel first and click on phpMyAdmin in Databases section.
Step 2: Create a TABLE in the same wordpress database. Below is the CREATE TABLE command:
CREATE TABLE tbl_leagues (
leagues_id char(8) NOT NULL,
leagues_name varchar(50) NOT NULL,
leagues_icon_sml varchar(500) DEFAULT NULL,
leagues_icon_tny varchar(500) DEFAULT NULL,
leagues_icon_mdm varchar(500) DEFAULT NULL,
leagues_upd_dt timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (leagues_id),
KEY leagues_name (leagues_name)
);
Click on GO now
Step 3: Once table is created go to same WordPress PHP page (API_Leagues.php) we created in part -2. We will load the API data into MySQL Table tbl_leagues which we just created. We will use WordPress wpdb class to communicate with MySQL. If you are interested to learn more about it click here

Deleting any record present in the table

  • We will delete the entries first from the table if it exists. This is required in order to avoid duplicate entries for same League in the MySQL Table.
$wpdb->delete(
‘tbl_leagues’,
array(
‘leagues_id’ => $response[‘items’][$var_cntr][‘id’]
),
array( ‘%s’)
);
In the above SQL command we are deleting any record present in the table which matches Leagues_Id value with the value we fetched via API.
  • Next we will insert the new value which we received from API call.
$wpdb->insert(
‘tbl_leagues’,
array(
‘leagues_id’ => $response[‘items’][$var_cntr][‘id’],
‘leagues_name’ => $response[‘items’][$var_cntr][‘name’],
‘leagues_icon_sml’ => $response[‘items’][$var_cntr][‘iconUrls’][‘small’],
‘leagues_icon_tny’ => $response[‘items’][$var_cntr][‘iconUrls’][‘tiny’],
‘leagues_icon_mdm’ => $response[‘items’][$var_cntr][‘iconUrls’][‘medium’]
),
array( ‘%s’,’%s’,’%s’,’%s’,’%s’ )
);
In the above SQL we insert the data into MySQL table “tbl_leagues” for all the 5 column values.

Add SQL commands

Next add the above 2 SQL commands in WHILE loop after you display value on the page:
Now transfer the updated php file into WordPress location and browse the same webpage again. You will see the data again as before on the webpage however this time you have stored the same data into MySQL Table too. Go to phpMyAdmin and select the table “Tbl_Leagues”.
Congratulations. Now you know how to pull data using Clash of Clans API and display it on a webpage. Also you now know how to store the data into MySQL Table.
Conclusion
We believe this 3 part tutorial should help you in creating fantastic applications to manage your COC Clans. We hope we were able to answer your basic queries regarding Clash of Clans API and how to use it.
Cheers !!!

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…