-1

不知道我应该如何放置标题,但这里是代码:

if(isset($stats->playerStatus->server->serverIPv6Address)){ // prefer the IPv6 address if it's there`     
    $srvDetails = file_get_contents('https://api.kag2d.com/server/ip/' . $stats->playerStatus->server->serverIPv6Address . '/port/' . $stats->playerStatus->server->serverPort . '/status');
}elseif(isset($stats->playerStatus->server->serverIPv4Address)){
    $srvDetails = file_get_contents('https://api.kag2d.com/server/ip/' . $stats->playerStatus->server->serverIPv4Address . '/port/' . $stats->playerStatus->server->serverPort . '/status');
}


$srvContent   = json_decode($srvDetails, true);
$srvName      = $srvContent->serverName;
$playerString = 'Players: ' . $srvContent->currentPlayers . '/' . $srvContent->maxPlayers;

并且 $srvName 保持为空并且 $playerString 只是 Players: / 以https://api.kag2d.com/server/ip/199.168.184.8/port/23002/status为例谢谢

4

1 回答 1

0

您没有创建对象,结果json_decode是一个数组,因为第二个参数为真。

删除该参数或将访问权限更改为数组样式。

使用基本的调试技能和调用var_dump($srvContent).

更新 以下代码适用于我:

$json = file_get_contents("https://api.kag2d.com/server/ip/199.168.184.8/port/23002/status");
var_dump($json);
$decoded = json_decode($json, true);
var_dump($decoded);
var_dump(json_last_error());

第二次转储的结果:

array(1) {
  'serverStatus' =>
  array(24) {
    'DNCycle' =>
    int(0)
    'DNState' =>
    int(0)
    'build' =>
    int(830)
    'connectable' =>
    int(1)
    'currentPlayers' =>
    int(2)
    'description' =>
    string(58) "Dreams, neon dreams, neon life, you will find, your, song."
    'firstSeen' =>
    string(19) "2012-07-10 18:26:53"
    'gameMode' =>
    string(15) "Team Deathmatch"
    'gameState' =>
    int(1)
    'gid' =>
    int(1)
    'gold' =>
    int(1)
    'lastUpdate' =>
    string(19) "2013-07-30 19:55:39"
    'mapH' =>
    int(122)
    'mapW' =>
    int(870)
    'maxPlayers' =>
    int(10)
    'maxSpectatorPlayers' =>
    int(0)
    'password' =>
    int(1)
    'playerList' =>
    array(2) {
      [0] =>
      array(1) {
        ...
      }
      [1] =>
      array(1) {
        ...
      }
    }
    'reservedPlayers' =>
    int(0)
    'serverIPv4Address' =>
    string(13) "199.168.184.8"
    'serverIPv6Address' =>
    string(17) "2001:470:4:727::6"
    'serverName' =>
    string(38) "TROUBLE - Beyond - password on forums."
    'serverPort' =>
    int(23002)
    'spectatorPlayers' =>
    int(0)
  }
}

那你呢?

于 2013-07-29T21:35:59.170 回答