PHP Json Terraria服务器状态

PHP Json Terraria服务器状态,php,imagettftext,Php,Imagettftext,我在google/stackoverflow上搜索了很长时间,没有找到我的答案。 我正在尝试放置一个图像,以查看我的Terraria服务器是否在线,如果在线,谁已连接,等等:p我对php真的很在行,但我正在努力学习!我从tshock.co上的geek85获得了这个源代码。以下是消息来源: <?php error_reporting(E_ERROR); //variables $ip = 'XXX.XXX.XXX.XXX'; $port = '7878'; /

我在google/stackoverflow上搜索了很长时间,没有找到我的答案。 我正在尝试放置一个图像,以查看我的Terraria服务器是否在线,如果在线,谁已连接,等等:p我对php真的很在行,但我正在努力学习!我从tshock.co上的geek85获得了这个源代码。以下是消息来源:

<?php
    error_reporting(E_ERROR);
    //variables
    $ip = 'XXX.XXX.XXX.XXX';
    $port = '7878'; //port of rest server, not terraria server
    $json = json_decode(file_get_contents('http://'.$ip.':'.$port.'/status/'), true);
    header('Content-Type: image/png');
    $im = @imagecreatefrompng('terraria_online.png');
    $font = 'visitor1.ttf';
    //$font2 = 'fonts/visitor2.ttf';
    $red = imagecolorallocate($im, 255, 24, 24);
    $blue = imagecolorallocate($im, 50,50,255);
    $black = imagecolorallocate($im, 10,10,10);
    $purple = imagecolorallocate($im, 127,0,127);
    $grey = imagecolorallocate($im, 50, 50, 50);
    $white = imagecolorallocate($im, 255, 255, 255);

    //server found and running
    if ($json['status'] == '200')
    {
        // text in the grey box
        $playerarray = explode(', ',$json['players']);
        imagettftext($im, 18, 0, 31, 16, $grey, $font, $json['name']);
        imagettftext($im, 18, 0, 30, 15, $red, $font, $json['name']);
        imagettftext($im, 14, 0, 31, 34, $grey, $font, 'IP: '.$ip);
        imagettftext($im, 14, 0, 30, 33, $black, $font, 'IP: '.$ip);
        imagettftext($im, 14, 0, 31, 49, $grey, $font, 'Port: '.$json['port']);
        imagettftext($im, 14, 0, 30, 48, $black, $font, 'Port: '.$json['port']);
        imagettftext($im, 14, 0, 9, 71, $grey, $font, 'Joueurs en ligne: '.$json['playercount']);
        imagettftext($im, 14, 0, 8, 70, $blue, $font, 'Joueurs en ligne: '.$json['playercount']);
        // show VIPs' avatar
        if ($json['playercount'] > 0)
        {
            //array of VIP players - (next step : a txt file with names and coordinates to parse...)
            $vip = array('QcSeb','Marlin','Twingo','Gaston');
            $num = count($vip);
            for ($i=0; $i<$num; $i++)
            {
                if (in_array($vip[$i], $playerarray))
                {
                    $player = @imagecreatefrompng('Images/'.strtolower($vip[$i]).'.png');//This line doesn't work for now
                    $long = imageSX($player);
                    $haut = imageSY($player);
                    switch ($vip[$i])
                    {
                        //here, it's a bit difficult: these are the coordinate where to place VIP players in the image.
                        case 'QcSeb':
                            $x = 350;
                            $y = 133;
                            break;
                        case 'Gaston':
                            $x = 300;
                            $y = 68;
                            break;
                        case 'Marlin':
                            $x = 261;
                            $y = 68;
                            break;
                        case 'Twingo':
                            $x = 307;
                            $y = 133;
                            break;
                    }
                    $ok = imagecopy($im,$player,$x,$y-$haut,0,0,$long,$haut);
                }
            }
            // name of players (VIP or not)
            $i = -1;
            $j = 0;
            // since my server only accept 8 players, I don't have any overflow control on the number of player's name to write but this code can do the work for any number of player (< to width of image / 75)
            foreach( $playerarray as $player )
            {
                if ($i++ >= 6)
                {
                    $i = 0;
                    $j++;
                }
                imagettftext($im, 11, 0, 11+($j*75), 101+($i*15), $grey, $font, $player);
                imagettftext($im, 11, 0, 10+($j*75), 100+($i*15), $purple, $font, $player);
            }
      }
    }
    // offline or not "statut == 200"
    else
    {
        $im = @imagecreatefrompng('terraria_offline.png');
        imagettftext($im, 18, 0, 11, 31, $purple, $font, 'IP:'.$ip.':'.$port);
        imagettftext($im, 18, 0, 10, 30, $grey, $font, 'IP:'.$ip.':'.$port);
        imagettftext($im, 42, 17, 16, 131, $purple, $font, 'offline');
        imagettftext($im, 42, 17, 15, 130, $black, $font, 'offline');
    }
    imagepng($im);
    imagedestroy($im);
    ?>
因为实际上只有我在服务器上

(XXX.XXX.XXX.XXX=我的隐藏IP:p)


下面是我尝试做的一个例子:


提前谢谢你的帮助

你能把问题说得更清楚些吗PRight现在我只能想到错误的文件,因为这个脚本没有输出任何json。在我的论坛tshock.co geek85分享了他的PHP源代码,得到了一个带有文本的图像,上面有“服务器IP、端口、在线/离线、玩家数量、玩家名称”,但当我进入页面时,我只得到了我的图像“teraria_Offline.png”:/此行用于获取有关$json=json解码的信息(文件内容('http://'.$ip':'.$port'./status/'),true);下面是我尝试做的一个例子:
{
  "status": "200",
  "name": "TShock Server",
  "port": "7777",
  "playercount": "1",
  "players": "QcSeb"
}