这是示例文件
<?php
include 'GameServerQuery.php';
echo '<pre>';
var_dump(GameServerQuery::querySource('178.33.129.216', 27022));
echo '</pre>';
?>
这是游戏服务器查询文件的一部分
public static function querySource($host, $port)
{
if ($reponse = self::ping($host, $port, "'xFF'xFF'xFF'xFFTSource Engine Query")) {
$cs15 = false;
$info = array();
$header = substr($reponse, 0, 5);
if ($header !== "'xFF'xFF'xFF'xFF'x6D" && $header !== "'xFF'xFF'xFF'xFF'x49") {
return false;
}
if ($reponse[4] == 'm') { //Le moteur CS1.5
$reponse = substr($reponse, 5);
$reponse = strstr($reponse, chr(0));
$cs15 = true;
}
elseif ($reponse[4] == 'I') { // Le moteur source
$reponse = substr($reponse, 5);
}
else {
return false;
}
$info['version'] = self::getByte($reponse);
$info['name'] = trim(self::getString($reponse));
$info['mapname'] = self::getString($reponse);
$info['gamedir'] = self::getString($reponse);
$info['gamedesc'] = self::getString($reponse);
if (!$cs15) $reponse = substr($reponse, 2);
$info['numplayers'] = self::getByte($reponse);
$info['maxplayers'] = self::getByte($reponse);
$info['bot'] = self::getByte($reponse);
$info['dedicated'] = (chr(self::getByte($reponse)) === 'd') ? 1 : 0;
$info['os'] = chr(self::getByte($reponse));
$info['password'] = self::getByte($reponse);
$info['secure'] = self::getByte($reponse);
return $info;
}
return false;
}
我的输出是
array(12) {
["version"]=>
int(0)
["name"]=>
string(28) "xGamers Winter Tournament #1"
["mapname"]=>
string(8) "de_dust2"
["gamedir"]=>
string(7) "cstrike"
["gamedesc"]=>
string(10) "Server ~ 1"
["numplayers"]=>
int(0)
["maxplayers"]=>
int(16)
["bot"]=>
int(47)
["dedicated"]=>
int(1)
["os"]=>
string(1) "l"
["password"]=>
int(0)
["secure"]=>
int(1)
}
我的问题是 ?例如,有没有办法让我以我想要的方式获得这些结果的输出。
MaxPlayers: 16
numPlayers: 0
Name: "xGamers Winter Tournament #1"
您的帮助将不胜感激,我不太擅长 php,但学习 php 是我的人生目标之一。
你在数组上使用var_dump
,你可以直接提取:
<?php
$array = GameServerQuery::querySource('178.33.129.216', 27022);
echo 'Name: '.$array['name'].'<br />';
echo 'numPlayers: '.$array['numplayers'];
/* etc ..... */
?>
或循环该数组:
<?php
$array = GameServerQuery::querySource('178.33.129.216', 27022);
foreach($array as $key => $arg){
echo $key.': '.$arg.'<br />';
}
?>
如果你只想在你的页面上输出示例,你需要制作一个小的输出模板。这应该有效:
$data = GameServerQuery::querySource('178.33.129.216', 27022);
echo 'MaxPlayers: ' . $data['maxplayers'] . '<br />';
echo 'numPlayers: ' . $data['numplayers'] . '<br />';
echo 'Name: ' . $data['name'] . '<br />';
不过,请确保您摆脱了标签。