Parsing the Kongregate Badge Feed with PHP

Back in September of last year, Jim Greer of Kongregate.com posted an article in the forums about a new badge data service served in JSON. This was great news for developers, we could finally grab data from our user accounts without having to do a web scrape on our user page.

However, this forum post seems to be the only official documentation on what is a very interesting data feed, so it is with this reason that I decided to create this beginner’s tutorial on how to use it.

Since Kongregate is a website that hosts Flash games, this data would primarily be used to collect data into such games – like showing user badges embedded into a Flash dashboard. In this tutorial, I will take a different approach and show you how to collect that data in PHP and build your own activity stream.

All Badges Feed

Located at http://www.kongregate.com/badges.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
  "description": "Collect 100 squiggles and pwn 10 spiders",
  "users_count": 285194,
  "name": "Running Start",
  "games": [
    {
      "url": "http://www.kongregate.com/games/DrNeroCF/fancy-pants-adventure-world-2",
      "title": "Fancy Pants Adventure: World 2"
    }
  ],
  "icon_url": "http://cdn2.kongregate.com/badge_icons/0000/0367/world2easy.png?1242937128",
  "created_at": "2008/01/24 20:26:15 -0800",
  "points": 5,
  "difficulty": "easy",
  "id": 184
}

This is an example of an item from the list of game badges in the feed. It contains more than enough information to help make your own application. Each element is labeled so that you can index and call each item when you need to. Further down I’ll show you how to sort out the data in PHP.

User Badges Feed

Can be found at http://www.kongregate.com/accounts/USERNAME/badges.json, replacing USERNAME with your account login name.

1
2
3
4
{
  "badge_id": 184,
  "created_at": "2008/06/23 06:20:42 -0700"
}

A much simpler looking JSON string. A user’s badge feed only contains a list of badge IDs and the date and time that the user achieved them. All other data can be found on the global badge feed above, we just match up the IDs.

Grabbing the Data

Last week I showed you how to fetch page content using PHP. We can reuse the get_page_content method here to collect our information from Kongregate. Be sure to add this method above all other code in your PHP file, before adding the content from the box below.

1
2
3
4
5
6
7
8
9
$all_badges_content = get_page_content(
  "http://www.kongregate.com/badges.json");
 
$all_badges = json_decode($all_badges_content, true);
 
$user_badges_content = get_page_content(
  "http://www.kongregate.com/accounts/USERNAME/badges.json");
 
$user_badges = json_decode($user_badges_content, true);

This sets up two arrays ($all_badges and $user_badges) that we can use to loop through and match IDs.

Displaying the Content

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
echo '<ol>';
foreach($user_badges as $badge)
{
  foreach($all_badges as $i => $global)
  {
    if($badge['badge_id'] == $global['id'])
    {
      echo
        '<li>'.
          '<a href="' . $global['games'][0]['url'] . '">'.
            htmlspecialchars( $global['name'], ENT_COMPAT, 'UTF-8' ) . ' Badge'.
          '</a>'.
          '<span>'.
            ' : ' . htmlspecialchars( $global['description'], ENT_COMPAT, 'UTF-8' ).
          '</span>'.
        '</li>';
        unset($badge[$i]);
        break;
    }
  }
}
echo '</ol>';

For each user badge in the $user_badges array we loop through our global badges to find a match and print an output of badge, link, and description to the page.

The break and unset in this code is useful for reducing memory usage on the script.

The break exits the current loop when the badges have been matched – since we won’t need to keep looking once we have found what we are looking for.

The unset removes the item from our list of all badges completely, since once it has been found once we won’t ever need to match it again since we can only have one of each badge.

Finishing Up

For a full copy-paste version of the code, feel free to skip to the end of this article, or view a demo page of this script.

Note: the demo page is a rendered example of the end result and does not update. For markup purposes only.

Also note that reading the JSON data directly from the server in this way is not recommended. For better use of this data, please store and manage the information within your own database setup.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
function get_page_content($url){
  $resource = curl_init();
 
  curl_setopt($resource, CURLOPT_URL, $url);
  curl_setopt($resource, CURLOPT_HEADER, false);
  curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
 
  $content = curl_exec($resource);
 
  curl_close($resource);
 
  return $content;
}
 
$all_badges_content = get_page_content(
  "http://www.kongregate.com/badges.json");
 
$all_badges = json_decode($all_badges_content, true);
 
$user_badges_content = get_page_content(
  "http://www.kongregate.com/accounts/USERNAME/badges.json");
 
$user_badges = json_decode($user_badges_content, true);
 
echo '<ol>';
 
foreach($user_badges as $badge)
{
  foreach($all_badges as $i => $global)
  {
    if($badge['badge_id'] == $global['id'])
    {
      echo
        '<li>'.
          '<a href="' . $global['games'][0]['url'] . '">'.
            htmlspecialchars( $global['name'], ENT_COMPAT, 'UTF-8' ) . ' Badge'.
          '</a>'.
          '<span>'.
            ' : ' . htmlspecialchars( $global['description'], ENT_COMPAT, 'UTF-8' ).
          '</span>'.
        '</li>';
        unset($badge[$i]);
        break;
    }
  }
}
echo '</ol>';
?>

One thought on “Parsing the Kongregate Badge Feed with PHP

Comments are closed.