Reputation: 111
I have the URL http://api.minetools.eu/ping/play.desnia.net/25565 which outputs statistics of my server.
For example:
{
"description": "A Minecraft Server",
"favicon": null,
"latency": 64.646,
"players": {
"max": 20,
"online": 0,
"sample": []
},
"version": {
"name": "Spigot 1.8.8",
"protocol": 47
}
}
I want to get the value of online player count to display it on my website as: Online Players: online amount
Can anyone help?
I tried to do:
<b> Online players:
<?php
$content = file_get_contents("http://api.minetools.eu/ping/play.desnia.net/25565");
echo ($content, ["online"]);
}
?>
</b>
But it didn't work.
Upvotes: 11
Views: 113745
Reputation: 11
Since it's returning an array you should use print_r or var_dump instead of echo. Or perhaps it threw you an error.
Upvotes: 1
Reputation: 3602
Your webservice (URL: http://api.minetools.eu/ping/play.desnia.net/25565) returns JSON.
This is a standard format, and PHP (at least since 5.2) supports decoding it natively - you'll get some form of PHP structure back from it.
Your code currently doesn't work (your syntax is meaningless on the echo
- and even if it was valid, you're treating a string copy of the raw JSON data as an array - which won't work), you need to have PHP interpret (decode) the JSON data first:
http://php.net/manual/en/function.json-decode.php
<?php
$statisticsJson = file_get_contents("http://api.minetools.eu/ping/play.desnia.net/25565");
$statisticsObj = json_decode($statisticsJson);
Your $statisticsObj
will be NULL
if an error occurred - and you can get that error using other standard PHP functions:
http://php.net/manual/en/function.json-last-error.php
Assuming it isn't NULL
, you can examine the structure of the object with var_dump($statisticsObj)
- and then alter your code to print it out appropriately.
In short, something like:
<?php
$statisticsJson = file_get_contents("http://api.minetools.eu/ping/play.desnia.net/25565");
$statisticsObj = json_decode($statisticsJson);
if ($statisticsObj !== null) {
echo $statisticsObj->players->online;
} else {
echo "Unknown";
}
You should also check what comes back from file_get_contents()
too - various return values can come back (which would blow up json_decode()
) on errors. See the documentation for possibilities:
http://php.net/manual/en/function.file-get-contents.php
I'd also wrap the entire thing in a function or class method to keep your code tidy. A simple "almost complete" solution could look like this:
<?php
function getServerStatistics($url) {
$statisticsJson = file_get_contents($url);
if ($statisticsJson === false) {
return false;
}
$statisticsObj = json_decode($statisticsJson);
if ($statisticsObj !== null) {
return false;
}
return $statisticsObj;
}
// ...
$stats = getServerStatistics($url);
if ($stats !== false) {
print $stats->players->online;
}
If you want better handling over server / HTTP errors etc, I'd look at using curl_*()
- http://php.net/manual/en/book.curl.php
Ideally you also should be confirming the structure returned from your webservice is what you expected before blindly making assumptions too. You can do that with something like property_exists()
.
Happy hacking!
Upvotes: 2
Reputation: 11375
1) Don't use file_get_contents()
(If you can help it)
This is because you'd need to enable fopen_wrappers
to enable file_get_contents()
to work on an external source. Sometimes this is closed (depending on your host; like shared hosting), so your application will break.
Generally a good alternative is curl()
2) Using curl()
to perform a GET
request
This is pretty straight forward. Issue a GET
request with some headers using curl()
.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://api.minetools.eu/ping/play.desnia.net/25565",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
3) Using the response
The response comes back in a JSON object. We can use json_decode()
to put this into a usable object or array.
$response = json_decode($response, true); //because of true, it's in an array
echo 'Online: '. $response['players']['online'];
Upvotes: 41
Reputation: 256
Your server is returning a JSON string. So you should use json_decode() function to convert that into a plain PHP object. Thereafter you can access any variable of that object.
So, something like this shall help
<?php
$content = file_get_contents("http://api.minetools.eu/ping/play.desnia.net/25565");
$result = json_decode($content);
print_r( $result->players->online );
?>
More details for json_decode can be read here - http://php.net/manual/en/function.json-decode.php
Upvotes: 9