Battlefield 2 Stats in PHP

I’ve had a couple of questions regarding my Battlefile 2 Stats in Python post, and how it may be possible to do the same in PHP, so I thought I’d add an update for that.

Simple PHP code for Battlefield 2 Stats retrieval:

<?php
	ini_set("user_agent","GameSpyHTTP/1.0");

	$info = "per*,cmb*,twsc,cpcp,cacp,dfcp,kila,heal,rviv,rsup,rpar,tgte,dkas,dsab,cdsc,rank,cmsc,kick,kill,deth,suic,ospm,klpm,klpr,dtpr,bksk,wdsk,bbrs,tcdr,ban,dtpm,lbtl,osaa,vrk,tsql,tsqm,tlwf,mvks,vmks,mvn*,vmr*,fkit,fmap,fveh,fwea,wtm-,wkl-,wdt-,wac-,wkd-,vtm-,vkl-,vdt-,vkd-,vkr-,atm-,awn-,alo-,abr-,ktm-,kkl-,kdt-,kkd-";

	$pid = '43595724';
	$data = file("http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=".$pid."&info=".$info);

	$stats = array_combine(explode("\\t", $data[3]), explode("\\t", $data[4]));

	printf("%s has %s kills and %s deaths and a score of %s", $stats['nick'], $stats['kill'], $stats['deth'], $stats['scor']);
?>

Note that if you’re not using PHP5, you’ll need to add the following drop-in replacement for the “array_combine” function:

	function array_combine($keys, $vals)
	{
		$i = 0;
		foreach ($keys as $key)
			$newarray[trim($key)] = trim($vals[$i++]);
		return $newarray;
	}

It’s also important to note that while at the time of writing this, this method of retrieving stats works, EA, DICE and GameSpy are supposedly working on a new XML-based stats system for BF2.

Updated: Since this was written, some things changed with the stats system, and the GameSpy application requires you to pass a bunch of columns you want info for. This can help customise the data you get back, so you only request what you need. I’ve included all the columns in the $info variable, which you can customise. Make sure it contains only valid columns, or you won’t get any data back at all.

For info on what to do with the stats, and what all the columns etc. mean, check out the BF2 Technical Wiki.

Technorati Tags: , ,

Comments

  1. October 17th, 2005 | 14:12

    And if you’re looking for rank information, adjust the Gamespy stats URL to look like this:

    http://bf2web.gamespy.com/ASP/getleaderboard.aspx?type=score&id=overall&pid=43595724

    Obviously change the PID to whoever’s stat’s you’re looking for :).

  2. October 17th, 2005 | 14:46

    thx very much

    nes-t

  3. October 17th, 2005 | 23:21

    hey is there somewhere a description where i can find for what the bytes are standing for. f.ex. the RANKING, I really can’t find anything to associate with the ranking

    regards

    NES_t

  4. nes-t
    October 17th, 2005 | 23:29

    ok sorry didnt read ur 2nd post =)

  5. nes-t
    October 18th, 2005 | 18:11

    hellou =) back again

    are there any more extensions of the
    http://bf2web.gamespy.com/ASP/getleaderboard.aspx?type=
    file

    like
    http://bf2web.gamespy.com/ASP/getleaderboard.aspx?type=time...

    or something else

    thx

    Nes-T

  6. nes-t
    October 18th, 2005 | 18:18

    And with the awards, can I get them all over these extensions?

    thx for the Help

    NES-T

  7. October 18th, 2005 | 22:29

    Dunno acually ;)
    Check out the BF2 Technical Info Wiki on the subject: http://bf2.fun-o-matic.org/index.php?title=BF2_Statistics

  8. NES-T
    October 19th, 2005 | 09:31

    ok thx for the link
    look interesting

    regards

    NES-T

  9. Matt
    July 14th, 2006 | 19:57

    I tried using the code above and receive an error:

    Warning: array_combine() [function.array-combine]: Both parameters should have equal number of elements in c:\Inetpub\wwwroot\test\test.php on line 11

    Any ideas?

  10. July 16th, 2006 | 09:47

    Hi

    For some reason when I pasted the code intot the blog, it dropped some characters.

    The line:
    $stats = array_combine(explode(”t”, $data[3]), explode(”t”, $data[4]));

    Should be:
    $stats = array_combine(explode(”\t”, $data[3]), explode(”\t”, $data[4]));

    Note the “\t”. :)

  11. FuX
    September 29th, 2006 | 12:37

    hi im trying to get this script to show player info for a certain kit(sniper) but all i get is a blank value.. hoping someone can assist me wit this.. im no php expert so i tried this out with no luck

    $data = file(”http://bf2web.gamespy.com/ASP/getplayerinfo.aspx?pid=”.$pid.”&info=”.$info.”&kit=7″);
    printf(”%s has %s kills and %s deaths and a score of %s KIT: %s”, $stats['nick'], $stats['kill'], $stats['deth'],

    $stats['scor'], $stats['kkl-']);

  12. Thalantyr
    January 6th, 2007 | 04:14

    Heya guys, just wondering if this still worked?

  13. January 15th, 2007 | 01:55

    does it work now???

  14. April 26th, 2007 | 17:14

    Yeah it works… but does anyone have a list of what the “codes” mean?

    this loops thru everything, but i need to know what all the codes mean

    “;
    }
    ?>

  15. Lt.Hendrix
    October 19th, 2007 | 23:04

    Here you can find more info about gathering data.
    http://bf2.fun-o-matic.org/index.php/BF2Stats

Leave a reply