Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: Clash of Clans API - a tutorial

  1. #1

    Lightbulb Clash of Clans API - a tutorial

    I have started to put together a tutorial on how to make a web page for your clan using the Clash of Clans API. This tutorial will be using PHP.

    What is the CoC API? : The Clash of Clans API provides near realtime access to game related data. In order to access the API, you need a developer account and a key for your application.

    Where do I find information on the API? : https://developer.clashofclans.com/ This is also where you register for your developer account and set up your key(s).

    The tutorial will cover 3 steps:
    1. Fetching game data from the API : link
    2. Displaying the data on the web : link
    3. Providing some style to the display : clan table ,
    Last edited by ufis; August 11th, 2016 at 02:41 PM.

  2. #2

    Fetch data using the API

    Below the source code to fetch data using the API:

    Explanation:
    header : sets the charset to UTF-8. This will cause the special characters to display properly
    <CLANTAG> : You need to replace this with your clan's tag (or any other clan you wish to fetch data for.
    <APITOKEN> : You need to replace this with the value of your API token.
    $url : This specific url fetches data for a specific clan. Other URLs are available from the documentation.
    curl_* : This us the PHP method we use to fetch the data. Read more at : http://php.net/manual/en/function.curl-init.php
    json_decode : The data from the API comes in a JSON string. This function decodes this string into a PHP array. http://php.net/manual/en/function.json-decode.php
    var_dump : This just dumps the decoded data between html pre tags to show all the data returned.

    Next we will display all of this in a HTML page.

    PHP Code:
     <?php
    header
    ('Content-Type: text/html; charset=UTF-8');

    $clantag "<CLANTAG>";

    $token "<APITOKEN>";

    $url "https://api.clashofclans.com/v1/clans/" urlencode($clantag);

    $ch curl_init($url);

    $headr = array();
    $headr[] = "Accept: application/json";
    $headr[] = "Authorization: Bearer ".$token;

    curl_setopt($chCURLOPT_HTTPHEADER$headr);
    curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
    curl_setopt($chCURLOPT_RETURNTRANSFER1); 

    $res curl_exec($ch);
    $data json_decode($restrue);
    curl_close($ch);

    echo 
    "<pre>";
    var_dump($data);
    echo 
    "</pre>";
    ?>
    The result will be similar to this (this is the #1 ranked global team at the time of posting this):
    Notice that at the end is the clan member data (only the first member displayed here to keep it short)
    Code:
    array(17) {
      ["tag"]=>
      string(9) "#YJLY82QG"
      ["name"]=>
      string(12) "大唐天子"
      ["type"]=>
      string(10) "inviteOnly"
      ["description"]=>
      string(251) "欢迎加入大唐天子,COC及CR审核微信:datang1719目标一致,努力奋斗!大唐天子COC交流群QQ:524599086;关注微博"大唐天子COC"及微信公众号。团结、和谐、互爱、自信,永远的部落!We are family!"
      ["location"]=>
      array(4) {
        ["id"]=>
        int(32000056)
        ["name"]=>
        string(5) "China"
        ["isCountry"]=>
        bool(true)
        ["countryCode"]=>
        string(2) "CN"
      }
      ["badgeUrls"]=>
      array(3) {
        ["small"]=>
        string(93) "https://api-assets.clashofclans.com/badges/70/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
        ["large"]=>
        string(94) "https://api-assets.clashofclans.com/badges/512/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
        ["medium"]=>
        string(94) "https://api-assets.clashofclans.com/badges/200/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
      }
      ["clanLevel"]=>
      int(7)
      ["clanPoints"]=>
      int(51222)
      ["requiredTrophies"]=>
      int(4200)
      ["warFrequency"]=>
      string(7) "unknown"
      ["warWinStreak"]=>
      int(0)
      ["warWins"]=>
      int(56)
      ["warTies"]=>
      int(2)
      ["warLosses"]=>
      int(47)
      ["isWarLogPublic"]=>
      bool(true)
      ["members"]=>
      int(50)
      ["memberList"]=>
      array(50) {
        [0]=>
        array(10) {
          ["tag"]=>
          string(9) "#99RV8CPY"
          ["name"]=>
          string(21) "༺✨等风来✨༻"
          ["role"]=>
          string(5) "admin"
          ["expLevel"]=>
          int(162)
          ["league"]=>
          array(3) {
            ["id"]=>
            int(29000022)
            ["name"]=>
            string(13) "Legend League"
            ["iconUrls"]=>
            array(3) {
              ["small"]=>
              string(94) "https://api-assets.clashofclans.com/leagues/72/R2zmhyqQ0_lKcDR5EyghXCxgyC9mm_mVMIjAbmGoZtw.png"
              ["tiny"]=>
              string(94) "https://api-assets.clashofclans.com/leagues/36/R2zmhyqQ0_lKcDR5EyghXCxgyC9mm_mVMIjAbmGoZtw.png"
              ["medium"]=>
              string(95) "https://api-assets.clashofclans.com/leagues/288/R2zmhyqQ0_lKcDR5EyghXCxgyC9mm_mVMIjAbmGoZtw.png"
            }
          }
          ["trophies"]=>
          int(5469)
          ["clanRank"]=>
          int(1)
          ["previousClanRank"]=>
          int(1)
          ["donations"]=>
          int(0)
          ["donationsReceived"]=>
          int(71)
        }
    ...
    Last edited by ufis; August 10th, 2016 at 09:20 PM.

  3. #3
    Trainee
    Join Date
    Sep 2015
    Location
    3"Ace
    Posts
    36
    Quote Originally Posted by ufis View Post
    Below the source code to fetch data using the API:

    Explanation:
    header : sets the charset to UTF-8. This will cause the special characters to display properly
    <CLANTAG> : You need to replace this with your clan's tag (or any other clan you wish to fetch data for.
    <APITOKEN> : You need to replace this with the value of your API token.
    $url : This specific url fetches data for a specific clan. Other URLs are available from the documentation.
    curl_* : This us the PHP method we use to fetch the data. Read more at : http://php.net/manual/en/function.curl-init.php
    json_decode : The data from the API comes in a JSON string. This function decodes this string into a PHP array. http://php.net/manual/en/function.json-decode.php
    var_dump : This just dumps the decoded data between html pre tags to show all the data returned.

    Next we will display all of this in a HTML page.

    PHP Code:
     <?php
    header
    ('Content-Type: text/html; charset=UTF-8');

    $clantag "<CLANTAG>";

    $token "<APITOKEN>";

    $url "https://api.clashofclans.com/v1/clans/" urlencode($clantag);

    $ch curl_init($url);

    $headr = array();
    $headr[] = "Accept: application/json";
    $headr[] = "Authorization: Bearer ".$token;

    curl_setopt($chCURLOPT_HTTPHEADER$headr);
    curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
    curl_setopt($chCURLOPT_RETURNTRANSFER1); 

    $res curl_exec($ch);
    $data json_decode($restrue);
    curl_close($ch);

    echo 
    "<pre>";
    var_dump($data);
    echo 
    "</pre>";
    ?>
    The result will be similar to this (this is the #1 ranked global team at the time of posting this):
    Notice that at the end is the clan member data (only the first member displayed here to keep it short)
    Code:
    array(17) {
      ["tag"]=>
      string(9) "#YJLY82QG"
      ["name"]=>
      string(12) "大唐天子"
      ["type"]=>
      string(10) "inviteOnly"
      ["description"]=>
      string(251) "欢迎加入大唐天子,COC及CR审核微信:datang1719目标一致,努力奋斗!大唐天子COC交流群QQ:524599086;关注微博"大唐天子COC"及微信公众号。团结、和谐、互爱、自信,永远的部落!We are family!"
      ["location"]=>
      array(4) {
        ["id"]=>
        int(32000056)
        ["name"]=>
        string(5) "China"
        ["isCountry"]=>
        bool(true)
        ["countryCode"]=>
        string(2) "CN"
      }
      ["badgeUrls"]=>
      array(3) {
        ["small"]=>
        string(93) "https://api-assets.clashofclans.com/badges/70/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
        ["large"]=>
        string(94) "https://api-assets.clashofclans.com/badges/512/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
        ["medium"]=>
        string(94) "https://api-assets.clashofclans.com/badges/200/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
      }
      ["clanLevel"]=>
      int(7)
      ["clanPoints"]=>
      int(51222)
      ["requiredTrophies"]=>
      int(4200)
      ["warFrequency"]=>
      string(7) "unknown"
      ["warWinStreak"]=>
      int(0)
      ["warWins"]=>
      int(56)
      ["warTies"]=>
      int(2)
      ["warLosses"]=>
      int(47)
      ["isWarLogPublic"]=>
      bool(true)
      ["members"]=>
      int(50)
      ["memberList"]=>
      array(50) {
        [0]=>
        array(10) {
          ["tag"]=>
          string(9) "#99RV8CPY"
          ["name"]=>
          string(21) "༺✨等风来✨༻"
          ["role"]=>
          string(5) "admin"
          ["expLevel"]=>
          int(162)
          ["league"]=>
          array(3) {
            ["id"]=>
            int(29000022)
            ["name"]=>
            string(13) "Legend League"
            ["iconUrls"]=>
            array(3) {
              ["small"]=>
              string(94) "https://api-assets.clashofclans.com/leagues/72/R2zmhyqQ0_lKcDR5EyghXCxgyC9mm_mVMIjAbmGoZtw.png"
              ["tiny"]=>
              string(94) "https://api-assets.clashofclans.com/leagues/36/R2zmhyqQ0_lKcDR5EyghXCxgyC9mm_mVMIjAbmGoZtw.png"
              ["medium"]=>
              string(95) "https://api-assets.clashofclans.com/leagues/288/R2zmhyqQ0_lKcDR5EyghXCxgyC9mm_mVMIjAbmGoZtw.png"
            }
          }
          ["trophies"]=>
          int(5469)
          ["clanRank"]=>
          int(1)
          ["previousClanRank"]=>
          int(1)
          ["donations"]=>
          int(0)
          ["donationsReceived"]=>
          int(71)
        }
    ...

    Thanks a lot, thats really help full


    Track and follow your favorite and best players in their local rankings.
    Trophyboard for Clash of Clans on Google Play Store.

  4. #4
    Super Member Zedharta's Avatar
    Join Date
    Aug 2016
    Posts
    898
    I really need this but I forgot what i've learn,tjme to study again,thanks for this,this is useful!

  5. #5

    Foreach or ForLoop through

    Quote Originally Posted by ufis View Post
    Below the source code to fetch data using the API:

    Explanation:
    header : sets the charset to UTF-8. This will cause the special characters to display properly
    <CLANTAG> : You need to replace this with your clan's tag (or any other clan you wish to fetch data for.
    <APITOKEN> : You need to replace this with the value of your API token.
    $url : This specific url fetches data for a specific clan. Other URLs are available from the documentation.
    curl_* : This us the PHP method we use to fetch the data. Read more at : http://php.net/manual/en/function.curl-init.php
    json_decode : The data from the API comes in a JSON string. This function decodes this string into a PHP array. http://php.net/manual/en/function.json-decode.php
    var_dump : This just dumps the decoded data between html pre tags to show all the data returned.

    Next we will display all of this in a HTML page.

    PHP Code:
     <?php
    header
    ('Content-Type: text/html; charset=UTF-8');

    $clantag "<CLANTAG>";

    $token "<APITOKEN>";

    $url "https://api.clashofclans.com/v1/clans/" urlencode($clantag);

    $ch curl_init($url);

    $headr = array();
    $headr[] = "Accept: application/json";
    $headr[] = "Authorization: Bearer ".$token;

    curl_setopt($chCURLOPT_HTTPHEADER$headr);
    curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
    curl_setopt($chCURLOPT_RETURNTRANSFER1); 

    $res curl_exec($ch);
    $data json_decode($restrue);
    curl_close($ch);

    echo 
    "<pre>";
    var_dump($data);
    echo 
    "</pre>";
    ?>
    The result will be similar to this (this is the #1 ranked global team at the time of posting this):
    Notice that at the end is the clan member data (only the first member displayed here to keep it short)
    Code:
    array(17) {
      ["tag"]=>
      string(9) "#YJLY82QG"
      ["name"]=>
      string(12) "大唐天子"
      ["type"]=>
      string(10) "inviteOnly"
      ["description"]=>
      string(251) "欢迎加入大唐天子,COC及CR审核微信:datang1719目标一致,努力奋斗!大唐天子COC交流群QQ:524599086;关注微博"大唐天子COC"及微信公众号。团结、和谐、互爱、自信,永远的部落!We are family!"
      ["location"]=>
      array(4) {
        ["id"]=>
        int(32000056)
        ["name"]=>
        string(5) "China"
        ["isCountry"]=>
        bool(true)
        ["countryCode"]=>
        string(2) "CN"
      }
      ["badgeUrls"]=>
      array(3) {
        ["small"]=>
        string(93) "https://api-assets.clashofclans.com/badges/70/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
        ["large"]=>
        string(94) "https://api-assets.clashofclans.com/badges/512/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
        ["medium"]=>
        string(94) "https://api-assets.clashofclans.com/badges/200/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
      }
      ["clanLevel"]=>
      int(7)
      ["clanPoints"]=>
      int(51222)
      ["requiredTrophies"]=>
      int(4200)
      ["warFrequency"]=>
      string(7) "unknown"
      ["warWinStreak"]=>
      int(0)
      ["warWins"]=>
      int(56)
      ["warTies"]=>
      int(2)
      ["warLosses"]=>
      int(47)
      ["isWarLogPublic"]=>
      bool(true)
      ["members"]=>
      int(50)
      ["memberList"]=>
      array(50) {
        [0]=>
        array(10) {
          ["tag"]=>
          string(9) "#99RV8CPY"
          ["name"]=>
          string(21) "༺✨等风来✨༻"
          ["role"]=>
          string(5) "admin"
          ["expLevel"]=>
          int(162)
          ["league"]=>
          array(3) {
            ["id"]=>
            int(29000022)
            ["name"]=>
            string(13) "Legend League"
            ["iconUrls"]=>
            array(3) {
              ["small"]=>
              string(94) "https://api-assets.clashofclans.com/leagues/72/R2zmhyqQ0_lKcDR5EyghXCxgyC9mm_mVMIjAbmGoZtw.png"
              ["tiny"]=>
              string(94) "https://api-assets.clashofclans.com/leagues/36/R2zmhyqQ0_lKcDR5EyghXCxgyC9mm_mVMIjAbmGoZtw.png"
              ["medium"]=>
              string(95) "https://api-assets.clashofclans.com/leagues/288/R2zmhyqQ0_lKcDR5EyghXCxgyC9mm_mVMIjAbmGoZtw.png"
            }
          }
          ["trophies"]=>
          int(5469)
          ["clanRank"]=>
          int(1)
          ["previousClanRank"]=>
          int(1)
          ["donations"]=>
          int(0)
          ["donationsReceived"]=>
          int(71)
        }
    ...
    How can you for each, or for loop through several clans, when clantag is an array? Is that recommended to do with this example?

    $clantag = array("clanone", "clantwo", "clanthree");

    $url[1] = "https://api.clashofclans.com/v1/clans/" . urlencode($clantag[1]);
    $url[2] = "https://api.clashofclans.com/v1/clans/" . urlencode($clantag[2]);
    $url[3] = "https://api.clashofclans.com/v1/clans/" . urlencode($clantag[3]);
    //How would you apply the Foreach / For Loop here with the array? I tried few methods, no success.
    $ch = curl_init($url);
    $headr = array();
    $headr[] = "Accept: application/json";
    $headr[] = "Authorization: Bearer ".$token;
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($ch);
    $data = json_decode($res, true);
    curl_close($ch);
    Last edited by AcidSnake; August 6th, 2016 at 04:27 PM.

  6. #6

    Exclamation Displaying the data on the web

    Below the code to display the data (see Fetch data using the API)

    Explanation:
    Remove php header function as we are now using HTML to set the charset
    Remove var_dump function as we are now using HTML to display the results
    Add a way to display any error with fetching data from API
    Add the clan name to the <title> tag
    Add the clan data (as defined in $data variable) to a set <table>
    Loop through the member list (as defined by $members) and add to <tr> tag within a <table> for members of the clan
    More reading on: php arrays, isset, echo, foreach, HTML5

    You will notice that this display is crude and not looking good (the forum display is also not 100% accurate, but it gives you the idea). So next we will provide some style to the display.

    PHP Code:
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
    <?php
    $clantag 
    "<CLANTAG>";

    $token "<APITOKEN>";

    $url "https://api.clashofclans.com/v1/clans/" urlencode($clantag);

    $ch curl_init($url);

    $headr = array();
    $headr[] = "Accept: application/json";
    $headr[] = "Authorization: Bearer ".$token;

    curl_setopt($chCURLOPT_HTTPHEADER$headr);
    curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
    curl_setopt($chCURLOPT_RETURNTRANSFER1); 

    $res curl_exec($ch);
    $data json_decode($restrue);
    curl_close($ch);

    if (isset(
    $data["reason"])) {
      
    $errormsg true;
    }

    $members $data["memberList"];

    ?>
      <title><?php echo $data["name"]; ?></title>
    </head>
    <body>
    <?php
      
    if (isset($errormsg)) {
        echo 
    "<p>""Failed: "$data["reason"], " : ", isset($data["message"]) ? $data["message"] : """</p></body></html>";
        exit;
      }
    ?>
      <table border="1">
        <tr>
          <td rowspan="11">Clan level : <?php echo $data["clanLevel"]; ?><br/><img src="<?php echo $data["badgeUrls"]["medium"]; ?>" alt="<?php echo $data["name"]; ?>"/></td>
          <td><?php echo $data["name"]; ?></td><td><?php echo $data["tag"]; ?></td>
          <td rowspan="11"><?php echo $data["description"]; ?></td>
        </tr>
        <tr>
          <td>Total points</td><td><?php echo $data["clanPoints"]; ?></td>
        </tr>
        <tr>
          <td>Wars won</td><td><?php echo $data["warWins"]; ?></td>
        </tr>
        <tr>
          <td>War win streak</td><td><?php echo $data["warWinStreak"]; ?></td>
        </tr>
        <tr>
          <td>Wars drawn</td><td><?php echo $data["warTies"]; ?></td>
        </tr>
        <tr>
          <td>Wars lost</td><td><?php echo $data["warLosses"]; ?></td>
        </tr>
        <tr>
          <td>Members</td><td><?php echo $data["members"]; ?>/50</td>
        </tr>
        <tr>
          <td>Type</td><td><?php echo $data["type"]; ?></td>
        </tr>
        <tr>
          <td>Required trophies</td><td><?php echo $data["requiredTrophies"]; ?></td>
        </tr>
        <tr>
          <td>War frequency</td><td><?php echo $data["warFrequency"]; ?></td>
        </tr>
        <tr>
          <td>Clan location</td><td><?php echo $data["location"]["name"]; ?></td>
        </tr>
      </table>
      <table border="1">
    <?php
      
    foreach ($members as $member) {
    ?>
        <tr>
          <td><?php echo $member["clanRank"], "("$member["previousClanRank"], ")"?></td>
          <td><img src="<?php echo $member["league"]["iconUrls"]["tiny"]; ?>" alt="<?php echo $member["league"]["name"]; ?>"/></td>
          <td><?php echo $member["expLevel"]; ?></td>
          <td><?php echo "<b>"$member["name"], "</b><br/>"$member["role"]; ?></td>
          <td>Donated:<br/><?php echo $member["donations"]; ?></td>
          <td>Received:<br/><?php echo $member["donationsReceived"]; ?></td>
          <td><?php echo $member["trophies"]; ?></td>
        </tr>
    <?php
      
    }
    ?>
      </table>
    </body>
    </html>
    The result will look similar to this, although the forum display is slightly different. This was the #1 ranked clan at the time of posting. The list of members has been kept to 2 to keep it short.

    Clan level : 10
    Q8 Army #P8PPLJ2 ����Welcome to Q8 Army���� Kuwaity and Proud���� the numbers speaks for it's self������ All Legends who like to push for trophies are welcome���� We are the champions �� LEGENDS ONLY ! instagram: q8_army_clan
    Total points 56202
    Wars won 177
    War win streak 0
    Wars drawn
    Wars lost
    Members 50/50
    Type inviteOnly
    Required trophies 4200
    War frequency lessThanOncePerWeek
    Clan location International
    1(0) 175 Latini
    coLeader
    Donated:
    29
    Received:
    178
    5828
    2(0) 164 Red Master
    member
    Donated:
    1
    Received:
    142
    5788
    ...
    Last edited by ufis; August 11th, 2016 at 02:40 PM.

  7. #7
    What if you had more than one clan to reveal. Say Clan A, Clan B, and Clan C; and you want three different tables for each. I understand the concept $data['object'], but not understanding how to get the $data for clan A, B, C. That means I would be making 3 API calls at once and trying to display clan A on one table, clan B on another table, and C on another table. I would just need to know the php part, as shown below, to run and gather A, B, and C clan information.

    Any information on how to reveal more than one clan will be solve my confusion on how to do that. I'm not sure if the code below would allow it, or might be something completely different recommended by you, but i figured to ask you guys if there was an easier method before I get crazy with php functions to make separate API calls for clash of clans A, B, C. There are 3 clans to run through.

    Would you run the API call 3 different times, or is there a for/foreach loop that can be used to run the clan tags ID through the example below?


    Thank you for your time! Greatly Appreciated. You Guys Rock!


    Example:
    $clantags = array("clanA", "clanB", "clanC");

    $url[1] = "https://api.clashofclans.com/v1/clans/" . urlencode($clantags[1]);//Clan A
    $url[2] = "https://api.clashofclans.com/v1/clans/" . urlencode($clantags[2]);//Clan B
    $url[3] = "https://api.clashofclans.com/v1/clans/" . urlencode($clantags[3]);//Clan C

    $ch = curl_init($url);// I think this has to be 3 different variables for clan tag + URL.

    $headr = array();
    $headr[] = "Accept: application/json";
    $headr[] = "Authorization: Bearer ".$token;

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $res = curl_exec($ch);// I think this has to be 3 different variables; 1 variable per clan
    $data = json_decode($res, true);// I think this has to be 3 different variables; 1 variable per clan
    curl_close($ch);

    Last edited by AcidSnake; August 11th, 2016 at 07:28 AM.

  8. #8
    Quote Originally Posted by AcidSnake View Post
    Any information on how to reveal more than one clan will be helpful.
    There are some of the other API calls that will return more than 1 clan. For example: https://api.clashofclans.com/v1/loca...clans?limit=10 will return the top 10 global clans. Check the API documentation for more.

    All of these that return multiple clans however only has the clan information, and does not include the player information too. For this you will have to make multiple calls to https://api.clashofclans.com/v1/clans/<CLANTAG> for every clan for which you require the information. I will share my solution to this late in this tutorial - for now one step at a time Meanwhile you can read up on curl_multi_exec to make multiple curl calls to the API, but read properly and look out for CPU usage pitfalls and CoC API throttling if you request too many too fast.

    For the top 10 listing above an array will be produced looking like this (snipped to keep it short)

    Code:
    array(2) {
      ["items"]=>
      array(10) {
        [0]=>
        array(9) {
          ["tag"]=>
          string(9) "#YJLY82QG"
          ["name"]=>
          string(12) "大”天♥"
          ["location"]=>
          array(4) {
            ["id"]=>
            int(32000056)
            ["name"]=>
            string(5) "China"
            ["isCountry"]=>
            bool(true)
            ["countryCode"]=>
            string(2) "CN"
          }
          ["badgeUrls"]=>
          array(3) {
            ["small"]=>
            string(93) "https://api-assets.clashofclans.com/badges/70/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
            ["large"]=>
            string(94) "https://api-assets.clashofclans.com/badges/512/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
            ["medium"]=>
            string(94) "https://api-assets.clashofclans.com/badges/200/C9eIs5Fps7MNSdnWQnLr6_i_Owb4vvMS42Tid6n4hOM.png"
          }
          ["clanLevel"]=>
          int(7)
          ["members"]=>
          int(50)
          ["clanPoints"]=>
          int(56272)
          ["rank"]=>
          int(1)
          ["previousRank"]=>
          int(1)
        }
        [1]=>
        array(9) {
          ["tag"]=>
          string(8) "#P8PPLJ2"
          ["name"]=>
          string(7) "Q8 Army"
          ["location"]=>
          array(3) {
            ["id"]=>
            int(32000006)
            ["name"]=>
            string(13) "International"
            ["isCountry"]=>
            bool(false)
          }
          ["badgeUrls"]=>
          array(3) {
            ["small"]=>
            string(93) "https://api-assets.clashofclans.com/badges/70/5x8bqHPl-2sM1SFb9ICz4_l4CCDFHUrxpMeCHyqQImA.png"
            ["large"]=>
            string(94) "https://api-assets.clashofclans.com/badges/512/5x8bqHPl-2sM1SFb9ICz4_l4CCDFHUrxpMeCHyqQImA.png"
            ["medium"]=>
            string(94) "https://api-assets.clashofclans.com/badges/200/5x8bqHPl-2sM1SFb9ICz4_l4CCDFHUrxpMeCHyqQImA.png"
          }
          ["clanLevel"]=>
          int(10)
          ["members"]=>
          int(49)
          ["clanPoints"]=>
          int(56114)
          ["rank"]=>
          int(2)
          ["previousRank"]=>
          int(2)
        }
    ...
      }
      ["paging"]=>
      array(1) {
        ["cursors"]=>
        array(1) {
          ["after"]=>
          string(14) "eyJwb3MiOjEwfQ"
        }
      }
    }
    The top level of your array will have 2 entries ["items"] and ["paging"]. Paging is used to fetch the next 10 should you wish to do it.
    The clan information sits in ["items"] with ["items"][0] being the first clan and ["items"][1] being the second and so on.
    So, to iterate or loop over all the clans you will have code similar to this:
    PHP Code:
    $data json_decode($restrue);
    $clans $data["items"];
    foreach (
    $clans as $clan) {
      echo 
    $clan["name"], " | "$clan["tag"], "<br/>";

    Last edited by ufis; August 11th, 2016 at 07:44 AM.

  9. #9

    Providing some style - Clan table

    After we have managed to display our clan information we need to provide some style to make our clan page look a bit better.

    Below the extracts of the changes needed to the code from the previous step:
    Change the <table border="1"> to <table class="clantable">
    Add the <span class="clanlevel"> tag around the Clan level entry. Do not forget the closing tag </span>

    PHP Code:
    ...
    <body>
    <?php
      
    if (isset($errormsg)) {
        echo 
    "<p>""Failed: "$data["reason"], " : ", isset($data["message"]) ? $data["message"] : """</p></body></html>";
        exit;
      }
    ?>
      <table class="clantable">
        <tr>
          <td rowspan="11"><span class="clanlevel">Clan level : <?php echo $data["clanLevel"]; ?></span><br/><img src="<?php echo $data["badgeUrls"]["medium"]; ?>" alt="<?php echo $data["name"]; ?>"/></td>
          <td><?php echo $data["name"]; ?></td><td><?php echo $data["tag"]; ?></td>
          <td rowspan="11"><?php echo $data["description"]; ?></td>
        </tr>
    ...
    Next we need to define the styles we want for this table. We will use CSS for this.
    In the <head> of the document we will add a <style> tag, right under the <title>, to look like this:
    PHP Code:
      <title><?php echo $data["name"]; ?></title>
      <style>
      /* add css styles here */
      </style>
    </head>
    Below the styles we will add for the table containing the clan information:
    body : <body> tag, here we set the font for the whole page. Search the internet for other fonts you can use.
    table.clantable : <table> tag with class "clantable". The dot (.) indicates class. Classes can be assigned to multiple elements. We specify the table with this class. Here we remove the borders from the table.
    table.clantable tr td : Again the table with class, followed by all <tr> and <td> children this table may have. Here we set the background colour on the <td> level. This is important to differentiate from setting the background on the <table> or <tr> level and will determine how we round corners amongst others.
    table.clantable tr:first-child td:nth-child(3n+1) : We select only the first <tr> element in this table and then the nth <td> child(ren) of that <tr>. nth here follows the formula of (3n+1) where n has the value 0, 1, 2, 3, ... This gives us the 1st and 4th <td>, which is the cells displaying the clan shield and description. Here we set the padding of these cells, 10px top and bottom, 20px left and right. We also set the alignment to top.
    table.clantable tr:first-child td:first-child : <table> with class, first <tr>, first <td>, giving us the cell with the clan shield and level. We set the alignment to center. We do this separately from the previous one because we do not want the description text centered.
    table.clantable tr:first-child td:last-child : This gives us the cell with the clan description. We set the width to prevent it from filling the entire width of the screen which would have made readin it more difficult.
    em vs px : see this explanation for which to use.
    table.clantable tr:first-child td:nth-child(2) : This gives us the cell with the clan name. We set various styles to give the name an unique look. We do the same, with a different look, for the next cell containing the clan tag.
    table.clantable tr:nth-child(1n+2) td : This gives us all the <td> from the 2nd <tr> and onwards. This will be all the clan data. We set the colour and font and spacing and also prevent text with spaces from spanning to a new line. We also draw a border line on the bottom of each cell.
    table.clantable tr:nth-child(1n+2) td:first-child : All the descriptions of the clan data. We align this to the left and give it some space. We do the same with the next entry, aligning the actual data to the right.
    table.clantable tr:first-child > td:first-child : This gives us the large cell containing the clan shield. We round the top left and bottom left corners of this cell. We do the same for the large cell containing the clan description, rounding the top right and bottom right corners.
    table.clantable .clanlevel : <table> with class and any component with the class "clanlevel" found in it. We give this a colour and background colour.

    Code:
      <style>
        body {
          font-family: Arial, Helvetica, sans-serif;
        }
        table.clantable {
          border-collapse: separate;
          border-spacing: 0px;
        }
        table.clantable tr td {
          background-color: #aaa;
        }
        table.clantable tr:first-child td:nth-child(3n+1) {
          padding: 10px 20px;
          vertical-align: top;
        }
        table.clantable tr:first-child td:first-child {
          text-align: center;
        }
        table.clantable tr:first-child td:last-child {
          width: 20em;
        }
        table.clantable tr:first-child td:nth-child(2) {
          color: #e3cfb5;
          font-size: 2em;
          font-weight: bold;
          text-shadow: 1px 1px 0px #000, -1px 1px 0px #000, 1px -1px 0px #000, -1px -1px 0px #000 ;
          padding-top: 5px;
          white-space: nowrap;
          text-align: left;
          padding-right: 50px;
          vertical-align: bottom;
        }
        table.clantable tr:first-child td:nth-child(3) {
          color: #4c4c4c;
          font-size: 1em;
          font-weight: bold;
          padding-top: 5px;
          white-space: nowrap;
          text-align: right;
          padding-left: 50px;
          vertical-align: bottom;
        }
        table.clantable tr:nth-child(1n+2) td {
          border-bottom: solid #000 1px;
          color: #fff;
          font-size: 1em;
          font-weight: bold;
          text-shadow: 1px 1px 0px #000;
          padding-top: 5px;
          white-space: nowrap;
        }
        table.clantable tr:nth-child(1n+2) td:first-child {
          text-align: left;
          padding-right: 50px;
        }
        table.clantable tr:nth-child(1n+2) td:last-child {
          text-align: right;
          padding-left: 50px;
        }
        table.clantable tr:first-child > td:first-child {
          border-top-left-radius: 15px;
          border-bottom-left-radius: 15px;
        }
        table.clantable tr:first-child > td:last-child {
          border-top-right-radius: 15px;
          border-bottom-right-radius: 15px;
        }
        table.clantable .clanlevel {
          color: #fff;
          font-size: 1em;
          font-weight: bold;
          background-color: #000;
          padding: 3px;
        }
      </style>
    When this is done, your clan data table will have a very similar look to how the clan is displayed in the actual game. The member data still need some work, and we will get to that next.

    Below a screenshot of what you can expect thus far. (showing the #1 ranked team at the time of posting)

    clanstyle.jpg
    Last edited by ufis; August 11th, 2016 at 02:46 PM.

  10. #10
    Thanks ufis Big Help here.
    Last edited by AcidSnake; August 19th, 2016 at 07:31 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •