Answer the question
In order to leave comments, you need to log in
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
},
Answer the question
In order to leave comments, you need to log in
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);
All your problems will be solved by Javascript and XMLHttpRequest
/ fetch
/ another request library.
After setInterval
every 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 php
or python
:)
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);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question