BattlelogApi v1.3

This is probably the biggest update to the API since it was introduced. Scratch that, this is the biggest update. Most of the changes have been structural. I have created several classes for different content types, split classes into their own files and created a proper class hierarchy. As far as usable features, I added award information (ribbons and medals) and server information. An unfortunate side effect of the cached language file (used for ribbons, medals, etc) is that the size of the API has increased from 5KB to 153KB (zipped sizes), but I think a larger file size is better than querying Battlelog for the language file every time (though the API allows you to so that too, albeit experimentally).

Hit up the project page for more information and the download link. Let me know if there are any issues with this release.

Small warning: When getting friends data (by invoking $soldier['friends']), note that the ID is not the nine-digit soldier ID. It is the main Battlelog user ID. I will be working on a way to remedy this situation if possible.

POSTED October 27, 2011 UNDER Projects

Comments

Rich said on October 27, 2011 at 11:21 pm:

Thanks so much for putting together such a wonderful and well organized API for BF3! You mention above that the ID is NOT the 9 digit soldier ID, but rather the ID that you can see in the URL when viewing a player’s stats page. This number also appears to be 9 digits long.

I wanted to see if there is an easy way to tell what is the actual Battlelog user ID opposed to the Soldier ID. As of right now, I am getting a 500 error with ‘Invalid soldier data’ as the message in my error log.

I double checked that I have PHP with cURL enabled, so I am not sure what else could be the issue. Thanks again!

    Artax said on October 28, 2011 at 9:09 am:

    @Rich, double check that your session cookie is being created, without it, you cannot query any soldier or server stats. Here’s some instructions:
    http://www.blakeharley.com/2011/10/battlelog-api-1-2-1-pre/

      Rich said on October 28, 2011 at 11:45 pm:

      Thanks so much for your help, I got this up and running now, it works awesome! Please let me know if there is anything that I can do to contribute to the project.

MrO1nk said on October 28, 2011 at 12:26 am:

Dude you rock. Thx for the major update.
I’ll share my work soon.

greybl00d said on October 28, 2011 at 4:12 am:

@rich Artax helped me on this one asswell look at the comments on version 1.2.1. this API keeps growing :) awesome work!!

    Rich said on October 28, 2011 at 11:50 pm:

    Thanks for pointing me in the right direction, that saved me a ton of time and was a super quick fix. Should have guessed it was something as simple as cookies.

Michael said on October 28, 2011 at 6:36 am:

nice work..

but is there no way to get this data without a battlelog login?
i dont want to risk my acc to be deletet due to reading stats for all my friends and publish it on our site ;)

    Blake said on October 28, 2011 at 5:24 pm:

    Unfortunately, no. Not at this time. I was hoping they would make stats public after the beta ended, but I guess that wasn’t the plan.

      Dan said on November 1, 2011 at 9:35 am:

      I wouldn’t be surprised that after the dust settles from launch, they’ll release an API of sorts.

MrO1nk said on October 28, 2011 at 7:04 am:

No idea, i mailed EA about it, but they have no clue what i asked.
They could only help me with the game, not with website related things.

Michael said on October 28, 2011 at 7:42 am:

i added mapMode below of map in the serverClass

‘mapMode’ => BF3Server::mode($data['mapMode']),

or was it accessible in an other way?

    Blake said on October 28, 2011 at 11:49 am:

    I forgot to put that in, thanks for catching that.

2PeU said on October 28, 2011 at 7:48 am:

Nice work so far, but is there a way to connect through this API with my user and to appear as I’m connected online to a server?
I know this is not very legal but I was just wondering if it’s possible.
Or do you have any advice on how to do this?

Thanks

Scott said on October 28, 2011 at 8:04 pm:

Getting a weird error when trying to request PS3 users. The example ID worked all well and good, but as soon as I replace with any PS3 user’s ID (I’ve tried 174501052 and 234040474), I wind up getting:

PHP Notice: Undefined index: awards in /opt/www/test/BF3Soldier.class.php on line 279
PHP Fatal error: Uncaught exception ‘BattlelogException’ with message ‘Invalid awards data’ in /opt/www/test/BF3Soldier.class.php:281
Stack trace:
#0 /opt/www/test/BF3Soldier.class.php(353): BF3Soldier->getAwards()
#1 /opt/www/test/BF3Soldier.class.php(378): BF3Soldier->get(‘ribbons’)
#2 /opt/www/test/test.php(6): BF3Soldier->debugData()
#3 {main}
thrown in /opt/www/test/BF3Soldier.class.php on line 281

shell returned 255

Any ideas?

    Blake said on October 28, 2011 at 8:48 pm:

    It looks like console players have their console name appended to the awards URL (eg, PS3 user’s stats URLs end with “ps3″).
    I’ll look into fixing that.

      Scott said on October 28, 2011 at 9:21 pm:

      As an FYI, that was using var_dump($soldier->debugData()), I noticed that with doing a basic print_r() function on $soldier also resulted in not pulling the stats. All I would get for my queries was their name, everything else would return 0′s. Digging a bit further, I noticed that viewing the source, on line 364, it would read: where the 4th value could be an variation of bf3-country-kit. If going with your idea of None, as long as the final value is 4 instead of 1, at least with the couple that I checked, it would work. Not certain as to the difference in the final value, but it seems significant enough.

        Scott said on October 28, 2011 at 9:23 pm:

        Sorry, silly me included the div tag, thus getting it cut out by the filter when posting. on line 364, the URL path was similar to: /bf3/overviewPopulateStats/[[ID]]/bf3-us-support/4/

          Blake said on October 29, 2011 at 3:41 am:

          I already know the URL structure. “bf3-us-support” is the user icon that gets displayed on the stats page. I leave that to “None” because it’s not necessary for this API.
          The number at the end is the platform int. 1 being PC, 2 is for Xbox and 4 is for PS3.
          The problem is trying to find out what platform a given soldier ID is supposed to be tied to. At the moment, this seems impossible without getting the main user data.

gerhardoh said on October 30, 2011 at 2:45 pm:

Blake, do you know how to fix the xbox 360 issue for the soldier when I manualy typ in the player´s console? Because you said you know 2 is for xbox.

greybl00d said on October 31, 2011 at 10:48 am:

cool the server part works like a charm. is it possible to diplay user status on bl this is done with a color on the avatar picture green blue and grey.
The list itself looks like it displays all players that where in that specific round.Because sometimes it displays more players then there are server slots available
I looks like there are 3 stages online on server, online on battlelog and offline. Also on soldier will a full weapons stats be feasible
as well ?

awesome work sofar!!!!

Dan said on November 1, 2011 at 9:33 am:

Any change of putting this up on GitHub?

I’ve added a function to pull in Map images (well at least one size, not sure of what the other sizes are yet if there are any) and I’ve also started working on a Platoon class.

I’d contribute some.

Fe3lApAcUt said on November 1, 2011 at 12:34 pm:

Hi,

thank you very much for this awesome code!

I´m just getting an error on my site :

Fatal error: Uncaught exception ‘BattlelogException’ with message ‘Invalid soldier data’ in BF3Soldier.class.php:75 Stack trace: #0 BF3Container.class.php(114): BF3Soldier->_parseData(‘{“partial”:fals…’) #1 BF3Container.class.php(76): BF3Container->_load() #2 BattlelogApi.inc.php(129): BF3Container->__construct(Object(BattlelogApi), ’123456789′, true)

Version: 1.3
yesterday it worked fine. did dice/ea change anything?

    Fe3lApAcUt said on November 1, 2011 at 4:18 pm:

    Hi Guys,

    changing the cookie path made it. Im sorry.

MrO1nk said on November 1, 2011 at 4:43 pm:

anyone got a decent way to print out the players on a server, without the [3] array tags, just the plain text.
Been looking over it for a few days and cant seem to find it.

Jack_O_Neill-DK said on November 4, 2011 at 11:09 am:

Hi Blake, I was trying to retreve rank from this profile

http://battlelog.battlefield.com/bf3/soldier/P1mpKill3r/stats/341583534/
(Probberly a cheater, dont know) but then I try that, it get the wrong image for that rank in rankimage url

//Kind regards

MSCARD02 said on November 8, 2011 at 8:50 pm:

thank you so much for creating this! I am currently trying to transfer this to asp and was wondering if anyone could post(with fake data of course) what the final url you send to login looks like?
I appreciate it

Luggruff said on November 10, 2011 at 9:56 am:

Thanks for this! It’s awesome, already have a nice little personal mobile site up wich I use when playing and don’t want to go to my computer to check my stats out (:

I bet you’re already working on this, but I though I’d just mention a couple of things I’m really missing:
-Score achieved on current rank
-Score needed for current rank
-Score left until rankup
-Next unlock and score achieved / left for kits, rank and vehicles

Can’t say that there is much more that’s essential (in my case)

What is “pointsNeeded” by the way? Mine say 280 000. That much points needed until, what? /:

Oh and yeah: Why is login needed? Many apps out there wich don’t use it (android) and why can’t you use username instead of userID (like apps out there do).

Thanks again, keep up the good work!

Trevor said on November 15, 2011 at 5:52 am:

Is there any way to get a JSON output of a specific Battle Report?

Nick said on November 15, 2011 at 6:23 am:

Excellent, well thought-out API, thank you.

Getting the nine-digit soldier IDs for friends would be a major upgrade and my only wish for improvement so far.

Thanks again.

Gerben said on November 20, 2011 at 11:05 am:

Hey Blake,

Nice library.
In the Utilities class you can lookup ranks, but it stops at rank 45 because after that the URL changes to: Anyone seen this rank on the battlefields yet? Legit? http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/ranks/medium/ss1.png up to ss100. Might wanna tweak that!

Keep it up!

    Fe3lApAcUt said on November 27, 2011 at 8:44 am:

    Hey Gerben,

    my workaround for this problem is (see BattlelogAPI.inc.php; search public static function getRankImage($rank, $size = ‘small’) ):

    public static function getRankImage($rank, $size = ‘small’)
    {
    $validSizes = array(‘tiny’, ‘small’, ‘medium’, ‘large’);
    $size = strtolower($size);

    if (!in_array($size, $validSizes))
    {
    throw new BattlelogException(“Invalid size ‘$size’ specified”);
    }

    if ($rank>=’46′) {
    for ($i=0;$i<=100;$i++) {
    if ($rank==$i) {
    $rankimgcache = $rank-45;
    $rankimg = "http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/ranks/$size/ss$rankimgcache.png";
    break;
    }
    }
    } else {
    $rankimg = "http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/ranks/$size/r$rank.png";

    }

    return $rankimg;
    }

    blame on me! it´s very bad code but solved the problem until the next version will release!

      efor74 said on January 23, 2012 at 1:16 pm:

      WTF?


      if ($rank > 45)
      return “http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/ranks/$size/ss”.($rank – 45).”.png”;
      else
      return “http://battlelog-cdn.battlefield.com/public/profile/bf3/stats/ranks/$size/r$rank.png”;

Nick said on November 22, 2011 at 4:38 am:

Hi,

it seems that since the update today the cookies are not needed to fetch the JSON data from Battlelog. Any chance you could update the API?

And I was wondering if you had time to check out whether there’s a way to determine the friend’s nine-digit ID.

Thanks!

Artax said on November 22, 2011 at 11:39 am:

@Nick,

Here’s a How To to get your nine-digit ID:
http://www.bfclanwars.com/2011/11/02/getting-bf3-player-ids-and-registering/

To get a friends ID, go to a friend’s stats page by first clicking their name. Then click their name again that is next to their Rank and Dog tags. Now you’ll see their Stats & Progression and so on, should should now be able to follow the second half of the tutorial and get your friends ID from the URL of the webpage.

    Nick said on November 22, 2011 at 12:35 pm:

    Thanks Artax! I was hoping to do this through the API, rather than having to scrape the pages. But I guess this is the only way to do it for now…

Basti said on November 23, 2011 at 4:08 am:

Hi@,

it seems that something has changed with the Battlefield 3 Update…
We get this error while getting the server data:

Fatal error:
Uncaught exception ‘BattlelogException’ with message ‘Invalid server data’ in /home/USER/domains/DOMAIN.com/public_html/page/inc/BF3Server.class.php:50

Stack trace:
#0 /home/USER/domains/DOMAIN.com/public_html/page/inc/BF3Container.class.php(114): BF3Server->_parseData(‘{“partial”:fals…’)
#1 /home/USER/domains/DOMAIN.com/public_html/page/inc/BF3Container.class.php(76): BF3Container->_load()
#2 /home/USER/domains/DOMAIN.com/public_html/page/inc/BattlelogApi.inc.php(143): BF3Container->__construct(Object(BattlelogApi), ‘af8aee66-10f6-4…’, true)
#3 /home/USER/domains/DOMAIN.com/public_html/page/server.php(21): BattlelogApi->getBF3Server(‘af8aee66-10f6-4…’)
#4 /home/USER/domains/DOMAIN.com/public_html/page/index.php(5): include(‘/home/k106614/d…’)
#5 {main} thrown in /home/USER/domains/DOMAIN.com/public_html/page/inc/BF3Server.class.php on line 50

What is wrong there?! :(

Michael said on November 23, 2011 at 12:23 pm:

yeah. EA Told, that the stats are now accessible without a login, so the script should now be possible without an ea account.

Basti said on November 23, 2011 at 12:31 pm:

ah okay. does anybody know how to fix that? i try to get a solution maybe someone else is faster than me :D

Basti said on November 24, 2011 at 8:26 am:

okay strange result. on my localhost (XAMPP) it works perfectly but on my host the api returns me the same error as in another post here: http://www.blakeharley.com/2011/10/battlelogapi-v1-3/#comment-138 :(

Basti said on November 24, 2011 at 10:25 am:

Okay, i found the error… If I try it on localhost, it creates the cookie-file in my xampp directory. on my webserver it can’t write this cookie-file.

here my work-around:

1. Open “BattlelogApi.inc.php” and look for line 34.
Change it from:
“define(‘BLA_COOKIE_FILE’, ‘cookies.txt’);”
to
“define(‘BLA_COOKIE_FILE’, ‘./temp/cookies.txt’);”

2. Create an Folder named “temp” in your root directory with the CHMOD ’777′.

3. Open your “Server-Info-Page” once. (The API should have placed a file with the name “cookies.txt” in the temp folder)

4. Check if the cookies.txt was placed in the temp folder.

5. Change the CHMOD of the temp to ’755′

6. That’s it! ;)

Jazz said on November 29, 2011 at 7:58 pm:

Is it maybe possible to get the image of a current map that is playing on the server?

And is there data available to get the team(US/RU) the player is in when getting the playerslist?

Thanks for writing this API, very nice!

Zach said on December 2, 2011 at 9:10 am:

Great work! I was able to build a G15 Keyboard Applet with your API. One thing that would be a nice to have is the score till rank up. Here is a photo of what I accomplished using your API… thanks!

https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/384113_10151006054465221_736060220_21940808_526768123_n.jpg

    Tesla said on December 5, 2011 at 12:53 am:

    bonjour,
    S’il vous plaît, vous pouvez libérer votre G15 applet pour BF3?
    Je serai vraiment heureux si vous pouvez la partager avec les joueurs qui ont un clavier G15 ..
    J’espère que vous accepterez, Zach, thx à l’avance …
    À bientôt

    Tesla said on December 5, 2011 at 12:53 am:

    hello,
    Please, you can release your g15 applet for BF3?
    I’ll be really happy if you can share it with players that have a g15 keyboard ..
    I hope you will accept, Zach, thx in advance …
    See you soon

jhon_petter-br said on December 16, 2011 at 5:22 am:

Hello friend, is it possible, you get a script in php stats Battlefield 2 and Battlefield: Bad Company 2?
I’ma member of a clan of fanatical battlefields, and would like to put the performance of the three platforms in our site like development.
Thanks for your attention.

http://www.battlefield.ea.com/battlefield/bf2/
http://www.battlefieldbadcompany2.com/login

kidrek said on December 28, 2011 at 4:24 am:

Hi,
i have a problem to fetch PS3 player data.
it is possible now to parse PS3 player stats?
thx

Dave said on December 29, 2011 at 1:40 pm:

Thanks for the awesome API!

I seem to only get 0 as a result for most requests though, the only ones that work are ID and Name. Am I doing something wrong?

Jack said on January 31, 2012 at 2:04 pm:

Hi Blake.

Just wanted to ask if my e-mail of 29 January 2012 has arrived. Is it?

Problem:
But the charge time for more than 100 members has taken far too long.
About 40 seconds to load, so

$soldier['name'];
$play_status = $soldier['user']['presence']['isPlaying'];
$rankimages = $soldier['rankImage']['tiny'];

are completely loaded.

Jack

Jack said on February 2, 2012 at 1:29 am:

Hi.

Another problem:

Notice: Undefined index: isOnlineOrbit

$isOnline = $soldier['user']['presence']['isOnline']; works
$isOnlineOrbit = $soldier['user']['presence']['isOnlineOrbit']; does not work
$isPlaying = $soldier['user']['presence']['isPlaying']; works

foreach($data['profileCommon']['user']['presence'] as $k => $v)
{
if ($k != ‘userId’)
{
$this->_user['presence'][$k] = $v;
}
}

Where is the problem?

defunkt said on February 6, 2012 at 9:56 pm:

API FEATURE REQUEST:

Hi, ended up here because I was Googling for a way to automate the sending of a message via Battlelog. I want to add an in-game command for players on our server (via a PRoCon plug-in) to page an admin and figured BL would be the best way to immediately reach those who might be able to respond (even if they don’t have PRoCon running). When you’ve run out of other stuff to do…

Jack said on February 7, 2012 at 7:14 am:

Hello. Get answers here?

Jono said on February 20, 2012 at 8:29 am:

Hi there, is this still being supported?

Leave a Reply

Fields marked with an asterisk (*) are required.