Jump to content

Grabbing Data from an API


jadelantern

Recommended Posts

Hi guys...

 

I was at home and doing some coding and stumbled across and API for a computer game called Battlefield 4

http://api.bf4stats.com/api/playerInfo?plat=pc&name=1ApRiL&output=js;

 

I was checking it out and it is a LOT of data.  I was wondering if its possible to only pull certain data from the API, say for instance you only want to pull your user name, ID, and what game you are playing (those are the top 3 things).  How would you pull only those 3 stats and display that information on say a webpage for you?

 

thanks guys

 

JL

Link to comment
Share on other sites

Well, yes and no. The documentation for the API is located here: http://bf4stats.com/api

 

Read through that to determine what parameters to pass to only get the data you want. But, it's likely you will still get more data than you are after. So, just parse the return data to use the data you want to display. The url you provided has an 'output' parameter. Look at the documentation and the available output formats to determine which one you want to work with.

 

EDIT: I don't see anything in the returned data that includes what game the user is playing.

<?php
 
//Set player name for the query
$playerName = '1ApRiL';
//Get the data in JSON format
$raw_data = file_get_contents("http://api.bf4stats.com/api/playerInfo?plat=pc&name={$playerName}&output=json&opt=details");
//Decode the data into an object
$data = json_decode($raw_data);
//Get player object
$playerObj = $data->player;
 
echo "ID: {$playerObj->id}<br>\n";
echo "Name: {$playerObj->name}<br>\n";
 
?>

Note: the opt=details added to the URL will return only the 'basic' details - which include the name and id. The documentation includes more info on the other parameters that can be used.

Edited by Psycho
Link to comment
Share on other sites

Is the setup to grab the data the same for any API that you are looking to grab information from?

 

No. How an API works is totally up to the programmer that creates it (or the requirements they are given). They can make whatever input parameters they think are needed and can have whatever output formats that they want. That's why they (should) have documentation.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.