Parsing Axho’s WoW (World of Warcraft) JSON feed with PHP

PHP 13.1.2011 12 Comments

This post is a little bit different from most of my posts as it relates to a more personal project as opposed to professional. This will explain how to use PHP to parse a JSON feed.

If you play World of Warcraft and need to manage a guild website, then you know that the new community site Blizzard has put up wrecked the old API and all the code sites had to access and display item, player, guild information. While the community awaits a new set of API’s from Wizard, Axho has spent the time to scrape the community site and setup feeds for everyone to use. Big thanks to Axho for the feeds. You can find the feeds at the URL below.

http://wowfeeds.wipeitau.com/

What I’ve read on the forums though, is that there are alot of users who don’t have much experience with PHP or how to read and parse XML/JSON. Axho was good enough to provide a HTML output feed to meet that need, but we know that this is not enough when you want to truly customize the feed. I decided to produce this tutorial to show how to use PHP to parse this feed. For the purposes of this demo, we will be working with the Guild Activity Feed..

First you need to grab the feed data. To do this use the following line (replace the URL you see there with the one you generated from Axho’s site)
$json = file_get_contents('http://wowfeeds.wipeitau.com/GuildActivity.php?location=US&rn=Arygos&gn=House%20of%20Pain&callback=?');

$json is just a variable, you can call it whatever you like. This will hold the returned data from Axho’s feed.

Now, PHP has a great little function called json_decode. It basically takes a json feed and creates objects that makes it easy to use within PHP. You can read more about it here if you so like. PHP Manual – json_decode

The problem is that the JSON output that Axho’s feed outputs has a few characters that the json_decode function doesn’t like, mainly a “$” in the beginning and brackets around the entire feed. We need to strip those away.
// remove the brackets
$json = str_replace(')', '', $json);
$json = str_replace('(', '', $json);

// remove the first character ($)
$json = substr($json, 1);

Now we have a string from the feed that we can utilize the json_decode function. To do this, just add the following line.
$data = json_decode($json);

Voila! PHP magic parses the entire feed and you now have a bunch of objects inside the $data variable.

Now what do we do? Well, each feed will produce a different set of objects based on the names provided by the feed. In this case, we have our main object (inside the $data variable) and the key property “guildActivity”. This property actually contains a collection of guildActivity objects which are basically each activity that is spilled out. We want to loop this to output the activity for the guild.
foreach ($data->guildActivity as $activity)
{
}

This code loops through all the activities in the guildActivity and provides a new variable for each time through the loop called activity. Each activity has the following properties (my own descriptions, not Axho’s)
// name -> name of the character who is tied with the activity
// type -> what kind activity is this
// numId -> numId attached to the activity (you need this if you want to link to the WoW API)
// achText -> the text which typically follows the character to describe the activity
// achObjective -> the objective reached
// achImage -> the full URL fo the achievement image
// achTime -> when the achievement occured

So with that in mind, if we want to write out each line we do the following
foreach ($data->guildActivity as $activity)
{
echo '<img src="' . $activity->achImage . '">' .
$activity->name . ' ' .
$activity->achText . ' ' .
$activity->achObjective .
' (' . $activity->achTime . ') ' .
'<br />'
;
}

And that should do it for you! I provided a sample below which includes features to integrate WoWhead tooltips and links to the BattleNet site. Your output should look the same as this, but without the links.

Sample output of PHP parsing of Axho’s JSON WoW feed

If you want the entire source, you can download it below
Download source code for this tutorial

Please feel free to comment if you have any questions. Thanks!

12 Responses to "Parsing Axho’s WoW (World of Warcraft) JSON feed with PHP"

  1. Dude. You rock harder than Eddie Vedder.

  2. I’d love to pick your brain sometime… I have so many questions! Not sure if you care or not, but I was able to kinda get this to work with AJAX. I haven’t figured out how to format or anything… but this is what I have so far.

    $(document).ready(function(){
    $.getJSON(“http://wowfeeds.wipeitau.com/RealmStatus.php?location=US&rn=Arthas&callback=?”,function(result){
    $.each(result, function(i, field){
    $(“div”).append(field + ” “);
    });
    });
    });

    Anyway, if you ever are bored and feel like someone bothering you… I’d love to research these feeds farther.

  3. Doug

    Hi, I’m trying to add this to my website, but I keep getting a “Trying to get property of non-object” error.

    guildActivity as $activity){ //This Is The Line That Throws The Error
    echo ‘achImage . ‘”>’ .
    $activity->name . ‘ ‘ .
    $activity->achText . ‘ ‘ .
    $activity->achObjective .
    ‘ (‘ . $activity->achTime . ‘) ‘ .
    ”;
    }
    ?>

    I do also get an “Invalid argument supplied for foreach()”, though it seems like the same problem to me. Do I need to declare any variables here? Also, just to clarify, I used the JSON feed from the site, not the HTML or XML right?

    Thanks for your help.

    • Hey Doug,

      Yes, use the JSON feed from Axho’s site.

      As for the error, did you cut something from by mistake? The line that throws the error should read:

      foreach ($data->guildActivity as $activity)

      If that line was just a mistake in your copy and paste and you still receive that error, it seems like the decode didn’t work. Try ensuring the json link works but putting that in your browser. Can you post your feed link here and I can see what I can do.

  4. Funkylolo

    Hi and many thanks for your code ;)

    I have made a little tweak to your wowhead links by inserting the activity type. This way, links will work correctly for items as well:

    a href=”http://www.wowhead.com/’ . $activity->type . ‘=’ . $activity->numId . ‘”

  5. Funkylolo

    Oops, in my previous comment I forgot to add 2 string replacements required for the activity type to work with links by removing the capital letters:

    $json = str_replace(‘Achievement’, ‘achievement’, $json);
    $json = str_replace(‘Item’, ‘item’, $json);

  6. Doug

    Thanks for the response,

    Somehow part of the code got cut off in the copy-paste, I have no idea how that happened, I’ll put the code down below (Hopefully it wont get cut off this time). The feed I got was:

    http://wowfeeds.wipeitau.com/GuildActivity.php?location=US&rn=The%20Venture Co&gn=Ravenwolf&limit=10&output=HTML&callback=?

    It’s not my guild and I don’t play WarCraft, I’m doing this for a friend who sent me the link using the JSON feed, so I can’t really check it until I talk to him again. It loads in the browser and seems to work there, but not when I try to run it. Any help is appreciated, thanks.

    guildActivity as $activity){
    echo ‘achImage . ‘”>’ .
    $activity->name . ‘ ‘ .
    $activity->achText . ‘ ‘ .
    $activity->achObjective .
    ‘ (‘ . $activity->achTime . ‘) ‘ .
    ”;
    }
    ?>

  7. Doug

    And it got cut off again didn’t it? Thats a bug, plus when the link moved to the next line, it made it unclick-able, you have to copy & paste the whole thing, that’s wierd… This site doesn’t like me, does it? OK, one more try, I’ll add in a few spaces and get rid of the tags to see if it’ll post.

    //Get The URL (JSON Feed)
    $json = file_get_contents(“http://wowfeeds.wipeitau.com/GuildActivity.php?location=US&rn=The%20Venture Co&gn=Ravenwolf&limit=10&output=HTML&callback=?”);

    //Remove The Parenthesis
    $json = str_replace(‘)’, ”, $json);
    $json = str_replace(‘(‘, ”, $json);

    //Remove The First Character ($)
    $json = substr($json, 1);
    $data = json_decode($json);

    foreach ($data->guildActivity as $activity){
    echo ‘achImage . ‘”>’ .
    $activity->name . ‘ ‘ .
    $activity->achText . ‘ ‘ .
    $activity->achObjective .
    ‘ (‘ . $activity->achTime . ‘) ‘ .
    ”;
    }

  8. Doug

    Oh, I see now, it’s cutting off the html tags and the left & right arrow things (I dont know the name) of the ?php tag and the one in the foreach were paired and everything in the middle was removed. Also would explain the quote inconsistencies inside the foreach…

    OK, last time, I’ll remove the left arrows since it doesn’t seem to care about unpaired right ones. I don’t know if there is a code tag or something on this site that I don’t know about, but anyway here’s the last shot. Sorry for the 3-post spam.

    //Get The URL (JSON Feed)
    $json = file_get_contents(“http://wowfeeds.wipeitau.com/GuildActivity.php?location=US&rn=The%20Venture Co&gn=Ravenwolf&limit=10&output=HTML&callback=?”);

    //Remove The Parenthesis
    $json = str_replace(‘)’, ”, $json);
    $json = str_replace(‘(‘, ”, $json);

    //Remove The First Character ($)
    $json = substr($json, 1);

    $data = json_decode($json);

    foreach ($data->guildActivity as $activity){
    echo ‘img src=”‘ . $activity->achImage . ‘”>’ .
    $activity->name . ‘ ‘ .
    $activity->achText . ‘ ‘ .
    $activity->achObjective .
    ‘ (‘ . $activity->achTime . ‘) ‘ .
    ‘br />’;
    }

  9. Doug

    I don’t entirely understand what I did wrong, but it works now so I really don’t care. Thanks a lot!

Leave a Reply