I
I
Ilya Rodionov2017-07-10 09:59:31
PHP
Ilya Rodionov, 2017-07-10 09:59:31

How to get data from the API and display it on the Web?

Dear users, hello!
There was a need to implement some program that is not available on the Internet.
In more detail:
There is a game server where you want to track online. For example, here: https://minecraft-statistic.net/ru/server/mcmwc/st... The
online of this server is updated every 10 minutes, because the resource administrator decided so.
Tell me, is it possible to implement, by getting the api value and other manipulations, the output of this on your page and, if so, how.
For example:
we get the value of the online server by api from the site https://mcapi.ca/query/play.minesuperior.com/info , where play.minesuperior.com is the ip of the server.
in json format we need only one line:

{
    "status": true,
    "hostname": "play.minesuperior.com",
    "port": 25565,
    "ping": 60,
    "version": "1.9",
    "protocol": 4,
    "players": {
        "online": 27,
        "max": 1250
    },

namely, online
, then you need to access the same api every minute, and display the online values ​​in a graph on the page like what is https://minecraft-statistic.net/ru/server/mcmwc/st...
There are thoughts, how can this be implemented?
Using python, you can access api and get values ​​online, and then, for example, write them to a file or database, and php will take values ​​from the database every minute and show them to the site,
but then the question arises: will there be a dynamic page update And wouldn't it need to be reloaded every time?
If not, then maybe through php without python? but if in the first one I can do it, then there can be big problems with php: I have no idea how to implement it
If you have any ideas, suggestions or questions about what I wrote - write, I will answer everyone!
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stalker_RED, 2017-07-10
@Stalker_RED

cron - to run once a minute
curl - to get statistics.
json_decode - convert json to an array.
In the simplest case

$json = file_get_contents('https://mcapi.ca/query/play.minesuperior.com/info');
$data = json_decode($json);
$online = $data[0]['players']['online'];

echo $online;
// или запись в файл
file_put_contents('/path/to/my/file.txt', $online);

A
Alexander, 2017-07-10
@boratsagdiev

All your problems will be solved by Javascript and XMLHttpRequest/ fetch/ another request library.
After setIntervalevery minute, you make a request to the API and display the resulting value in the right place. And there will be a dynamic update and no need to kick phpor python:)

V
Victor L, 2017-07-10
@Fzero0

I can offer another option:
1) Create a Google Sheets table
2) Tools script editor add code

function getData() {
  const url = 'https://mcapi.ca/query/play.minesuperior.com/info';
  const response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
  var params = JSON.parse(response.getContentText());  
  var d = new Date();
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var n =sheet.getRange("A1").getValue()+2;
      sheet.getRange("A"+n).setValue(d);
      sheet.getRange("B"+n).setValue(params.players.online);
      sheet.getRange("A1").setValue(n-1);
}

3) Now you need to add a trigger
4) Well, the table is filled, we build a graph =>
ec341852110e4f56a8520b83ea963918.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question