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: PHP, Battlefield 2, Stats
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 :).
thx very much
nes-t
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
ok sorry didnt read ur 2nd post =)
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
And with the awards, can I get them all over these extensions?
thx for the Help
NES-T
Dunno acually
Check out the BF2 Technical Info Wiki on the subject: http://bf2.fun-o-matic.org/index.php?title=BF2_Statistics
ok thx for the link
look interesting
regards
NES-T
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?
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”.
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-']);
Heya guys, just wondering if this still worked?
does it work now???
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
“;
}
?>
Here you can find more info about gathering data.
http://bf2.fun-o-matic.org/index.php/BF2Stats