I am trying to pull data from a json file then print it out in a script I have to create an image with information about server status.
Here is my json data in serverstatus.json
[{"load":"high"},{"load":"low"},{"load":"low"},{"load":"low"}]
Here is my script
<?php
$url = 'https://api.shipsofwar.net/servers?apikey=hPANE457LsBGmCUCD8JtoX7df44T52rYHJ8Gu0ZU';
$content = file_get_contents($url);
$json = json_decode($content, true);
$image = imagecreatefrompng('status.png');
imagealphablending($image, true);
$fontsize = 20;
$font = 'CaviarDreams';
$color1 = imagecolorallocate($image, 42, 8, 145); //blue
$color2 = imagecolorallocate($image, 30, 50, 98); //light blue
$server[0] = 'PvP One EU';
$server[1] = 'PvE One USA';
$server[2] = 'PvP Two USA';
$server[3] = 'PvP 3 EU mirror';
$ping[0] = '';
$ping[1] = '';
$ping[2] = '';
$ping[3] = '';
//imagefttext("Image", "Font Size", "Rotate Text", "Left Position", "Top Position", "Font Color", "Font Name", "Text To Print");
foreach($json['data'] as $item)
{
//Determine the server status
if($item['status'] == 'working')
{
$color = imagecolorallocate($image, 7, 146, 44);//green
$serverstatus = 'Online';
}
if($item['status'] == 'update')
{
$color = imagecolorallocate($image, 242, 181, 77); //orange
$serverstatus = 'Update';
}
if($item['status'] == 'maintenence')
{
$color = imagecolorallocate($image, 30, 50, 98); //blue
$serverstatus = 'Maintenence';
}
if($item['status'] != ('working' || 'update' || 'maintenence'))
{
$color = imagecoloralolcate($image, 244, 55, 66);//red
$serverstatus = 'Offline';
}
}
foreach($json['data'] as $item2)
{
$load = $item2['load'];
$values[] = array(
'load' => $load,
);
file_put_contents('serverload.json', json_encode($values));
}
$json_file = file_get_contents("serverload.json");
$json_load = json_decode($json_file, true);
foreach ($json_load as $item3)
{
$lat = $item3['load'];
$latency[] = array(
'load' => $lat,
);
$ping = $lat;
}
for($i=0; $i<4; $i++)
{
$name = $server[$i];
imagefttext($image, $fontsize, 0, 20, (20*$i+30)+(20*$i), $color1, $font, $name);
//if($item['status'] != ('update' || 'maintenence'))
//{
imagefttext($image, $fontsize, 0, 260, (20*$i+30)+(20*$i), $color2, $font, $ping[$i]);
//}
imagefttext($image, $fontsize, 0, 340, (20*$i+30)+(20*$i), $color, $font, $serverstatus);
}
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
The issue I am having is displaying the info from the serverstatus.json file. I think my issue is not fully understanding how to add information to the $ping array from $lat. The way I am trying to get it to display is like below.
PvP One EU high Online
PvE One USA low Online
PvP Two USA low Online
PvP 3 EU mirror low Online
But it is displaying like this instead.
PvP One EU l Online
PvE One USA o Online
PvP Two USA w Online
PvP 3 EU mirror Online
I've been trying to understand this for a couple hours now and I have tried so many things but nothing seems to work right. I have been on stack for 3 hours now looking through every question similar to mine but have yet to find the answer or even a clue to the right answer. Any and all help is appreciated.
Thank you.
Everytime you just overwrite $ping
variable with:
$ping = $lat;
But you should add $lat
as an element of $ping
array:
foreach ($json_load as $item3)
{
$lat = $item3['load'];
$latency[] = array(
'load' => $lat,
);
$ping[] = $lat; // here, add []
}