Page 1 of 3 123 LastLast
Results 1 to 10 of 33

Thread: Clash of Clans API - a tutorial

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

    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.

  5. #5

    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.

  6. #6
    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.

  7. #7
    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.

  8. #8
    Hey,

    I have a problem, I am not very familiar with php so i don't know what to do. Here is what it says when I try to load the page: Notice: Undefined index: memberList in /opt/lampp/htdocs/clashofclans/index.php on line 31
    Failed: notFound :

    Thanks.

  9. #9
    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!

  10. #10
    Fresh Spawn
    Join Date
    Aug 2016
    Posts
    1
    Quote Originally Posted by ufis View Post
    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 ,
    @ufis, i has done following all three parts following this tutorial. And the results it's awesome, now i have one question. How to make clan level centering inside clan badge?


    Btw, thank's for who can answering my question..

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
  •