0

我的错误是:

注意:未定义的变量:在第 21 行的 C:\xampp\htdocs\server4.php 中返回

第 21 行是:

   $return .= fgets($this->_socket);

注意:未定义的偏移量:第 95 行 C:\xampp\htdocs\server4.php 中的 0

注意:未定义的偏移量:第 95 行 C:\xampp\htdocs\server4.php 中的 0

第 95 行是:

$serverInfo = $master->server_getInfo($serverList[0]['ip'], $serverList[0]['port']);

和我的完整代码:

<?php
class Q3Master
{
    private $_socket;
    private $_port = '20810';
    private $_host;

    public function __construct($masterHost)
    {
            $this->_socket = fsockopen('udp://79.175.173.73',20810);
            stream_set_blocking($this->_socket,0);
    }

    public function master_listServers($timeout = '1')
    {
            fputs($this->_socket,str_repeat(chr(255),4).'getservers 69 empty full demo'."\n");

            $time=time()+$timeout;
            while($time > time() || strpos($return,'EOT') === FALSE)
            {
                    $return .= fgets($this->_socket);
            }
            $return = explode('\\',$return);
            unset($return[0]);
            unset($return[count($return)]);
            $iplist = array();
            foreach($return as $server)
            {
                    for($i = 0;$i < 4;$i++)
                            $addr[] = ord($server[$i]);

                    for($i = 4;$i < 6;$i++)
                            $port .= dechex(ord($server[$i]));
                    $port = hexdec($port);
                    $iplist[] = array('ip' => join('.',$addr),'port' => $port);
                    unset($addr);
                    unset($port);
            }

            return $iplist;
    }

    function server_getInfo($adresse, $port)
    {
            if($port != 0)
            {
                    $cmd = "\xFF\xFF\xFF\xFFgetstatus";
                    $f = fsockopen('udp://'.$adresse, $port);

                    socket_set_timeout ($f, 1);
                    fwrite ($f, $cmd);
                    $data = fread ($f, 10000);
                    fclose ($f);

     if($data)
     {
        $temp = explode("\x0a",$data);

        $list3 = explode("\\",substr($temp[1],1,strlen($temp[1])));
        for ($i = 0;$i <= count($list3);$i = $i + 2) {
           $list[@$list3[$i]] = @$list3[$i + 1];
        }
        array_pop($list);

        $players = array();
        foreach($temp as $id => $player)
        {
           if($id != 0 AND $id != 1)
           {
              $infos = explode(' ', $player, 3);
              $name = explode('"', $infos[2]);
              $players[] = array('score' => $infos[0], 'ping' => $infos[1], 'name' => $name[1]);
           }
        }
        array_pop($players);

        $infos = array();
        $infos = $list;
        $infos['players'] = $players;

        return $infos;
     }
     else
        return FALSE;
            }
            else
                    return FALSE;
    }
}
$master = new Q3Master('79.175.173.73');

$serverList = $master->master_listServers();


$serverInfo = $master->server_getInfo($serverList[0]['ip'], $serverList[0]['port']);

?>
4

3 回答 3

0

您需要$return在使用它之前进行定义,因为如果while不执行循环$return未定义,因此不能用作explode().

$return = ''; // <-- define it as an empty string

while($time > time() || strpos($return,'EOT') === FALSE)
{
       $return .= fgets($this->_socket);
}
$return = explode('\\',$return);
于 2013-06-05T09:43:41.093 回答
0

注意:未定义的变量:在第 21 行的 C:\xampp\htdocs\server4.php 中返回

然后定义该变量:$return = null.

注意:未定义的偏移量:第 95 行 C:\xampp\htdocs\server4.php 中的 0

然后定义该 offset: $serverList[0] = array('ip' => null, 'port' => null),或者在使用它之前检查是否定义了偏移量:

if (isset($serverList[0])) {
  $serverInfo = $master->server_getInfo($serverList[0]['ip'],
                                        $serverList[0]['port']);
}

但首先你应该调查为什么你期望$return$serverList[0]被定义。也许这些变量应该由代码的不同部分提供,那是被破坏的。

于 2013-06-05T09:45:43.370 回答
0

使用以下命令添加 var 返回的初始化:

$return = '';

尝试这个:

public function master_listServers($timeout = '1')
    {
            $return = '';
            fputs($this->_socket,str_repeat(chr(255),4).'getservers 69 empty full demo'."\n");

            $time=time()+$timeout;
            while($time > time() || strpos($return,'EOT') === FALSE)
            {
                    $return .= fgets($this->_socket);
            }
            $return = explode('\\',$return);
            unset($return[0]);
            unset($return[count($return)]);
            $iplist = array();
            foreach($return as $server)
            {
                    for($i = 0;$i < 4;$i++)
                            $addr[] = ord($server[$i]);

                    for($i = 4;$i < 6;$i++)
                            $port .= dechex(ord($server[$i]));
                    $port = hexdec($port);
                    $iplist[] = array('ip' => join('.',$addr),'port' => $port);
                    unset($addr);
                    unset($port);
            }

            return $iplist;
    }
于 2013-06-05T09:42:52.700 回答