bf3stats API Call Example

As I have seen a couple of google searches hitting my page looking for some easy bf3stats API example here some code. It is pretty much the same in my bf3stats page and it gives you an idea how easy it is to fetch the data from their API.

 $postdata['players'] = json_encode($players); // Player names in an array
 $postdata['opt'] = json_encode($options); // Various options described on the bf3stats API page

$c = curl_init('http://api.bf3stats.com/pc/playerlist/');
 curl_setopt($c, CURLOPT_HEADER, false);
 curl_setopt($c, CURLOPT_POST, true);
 curl_setopt($c, CURLOPT_USERAGENT, 'BF3StatsAPI/0.1');
 curl_setopt($c, CURLOPT_HTTPHEADER, array('Expect:'));
 curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($c, CURLOPT_POSTFIELDS, $postdata);
 $data = curl_exec($c); // Holds you result as an array
 $statuscode = curl_getinfo($c, CURLINFO_HTTP_CODE);
 $errormsg = curl_error($c);
 curl_close($c);

 

Obviously this is just the CURL call without any error handling or additional checks. But that’s all there is to grabbing player data from the API. My page then uses all that data to come up with all the statistics you can see. For details refer to the bf3stats.class.php in the bf3stats project.