Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Wykorzystanie funkcji w skrypcie
Forum PHP.pl > Forum > Przedszkole
rcwarez
Mam problem, bo gdy używam kodu:

  1. class IVMPQuery
  2. {
  3. var $socket = false;
  4. var $ip = false;
  5. var $port = false;
  6.  
  7. function Query($ip,$port,&$errno,&$errstr,$timeout = 5,$gettimeout = 1)
  8. {
  9. $this->Close();
  10. $this->ip = $ip;
  11. $this->port = $port;
  12. $this->socket = @fsockopen('udp://'.$ip,$port,$errno,$errstr,$timeout);
  13. if($this->socket === false) return false;
  14. @stream_set_timeout($this->socket,$gettimeout);
  15. return true;
  16. }
  17.  
  18. function Close()
  19. {
  20. if($this->socket !== false)
  21. {
  22. fclose($this->socket);
  23. $this->socket = false;
  24. }
  25. }
  26.  
  27. function GetPacketData($command)
  28. {
  29. $packet = 'IVMP';
  30. $packet .= $command;
  31. return $packet;
  32. }
  33.  
  34. function ServerData()
  35. {
  36. fputs($this->socket,$this->GetPacketData('i'));
  37. @fread($this->socket,5); // IVMPi
  38.  
  39. $len = ord(@fread($this->socket,4));
  40. $hostname = @fread($this->socket,$len); // read hostname
  41. $players = ord(@fread($this->socket,4)); // read players
  42. $maxplayers = ord(@fread($this->socket,4)); // read max players
  43. $passworded = ord(@fread($this->socket,1)); // 1 byte for password
  44.  
  45. return array(
  46. 'hostname' => $hostname,
  47. 'players' => $players,
  48. 'maxplayers' => $maxplayers,
  49. 'passworded' => (bool)$passworded
  50. );
  51. }
  52.  
  53. function ServerRules()
  54. {
  55. fputs($this->socket,$this->GetPacketData('r'));
  56. @fread($this->socket,5); // IVMPr
  57.  
  58. $count = ord(@fread($this->socket,4));
  59. $arr = array();
  60. for($i = 0; $i < $count; $i++)
  61. {
  62. $len = ord(@fread($this->socket,4));
  63. $key = @fread($this->socket,$len);
  64. $len = ord(@fread($this->socket,4));
  65. $arr[$key] = @fread($this->socket,$len);
  66. }
  67.  
  68. return $arr;
  69. }
  70.  
  71. function Players()
  72. {
  73. fputs($this->socket,$this->GetPacketData('l'));
  74. @fread($this->socket,5); // IVMPl
  75.  
  76. $count = ord(@fread($this->socket,4));
  77. $arr = array();
  78. for($i = 0; $i < $count; $i++)
  79. {
  80. $id = ord(@fread($this->socket,4));
  81. $len = ord(@fread($this->socket,4));
  82. $arr[$id] = @fread($this->socket,$len);
  83. }
  84.  
  85. return $arr;
  86. }
  87.  
  88. // Default return is ms int. If $asfloat == true, will return as seconds and as a float
  89. function Ping($asfloat = false)
  90. {
  91. $time = microtime(true);
  92. fputs($this->socket,$this->GetPacketData('p'));
  93. @fread($this->socket,5); // IVMPp
  94. $reply = @fread($this->socket,4);
  95. $time = microtime(true) - $time;
  96. if(!$asfloat)
  97. {
  98. $time *= 1000;
  99. $time = (int)round($time);
  100. }
  101. if($reply == 'PONG')
  102. {
  103. return $time;
  104. }
  105.  
  106. return false;
  107. }
  108. }
  109.  
  110. $q = new IVMPQuery;
  111.  
  112. if(!$q->Query('80.82.23.2',9999,$errno,$errstr,2))
  113. {
  114. $status = "brak łączności";
  115. }
  116. else
  117. {
  118. $status = "Info: $q->ServerData() <br />Zasady: $q->ServerRules() <br />Graczy: $q->Players() <br />Ping: $q->Ping()";
  119. }


To wyświetla mi:
Info: ()
Zasady: ()
Graczy: ()
Ping: ()


Gdzieś popełniłem błąd przepisując kod?
$status ma być wykorzystywane w stylu (skryptu myBB).
Wszystko jest poprawnie niby, a napotykam to co w/w.
Adres: ivmpprojekt.cba.pl
nospor
$status = "Info: ".$q->ServerData()." <br />Zasady: ".$q->ServerRules()." <br />Graczy: ".$q->Players()." <br />Ping: ".$q->Ping();
Warto by zajrzeć do manuala i doczytać podstawy obsługi stringów
Gość
Dzięki.
Teraz efekt wygląda tak, po poprawce nospor-a.
Info: Array
Zasady: Array
Graczy: Array
Ping:


Sprawdzanie statusu serwera napotkało błąd?
Tak nie powinno być.
nospor
Wygląda na to, że $q->ServerData() zwraca tablicę a ty myślałeś że jakąś wartość.
Zrob sobie:
$zm = $q->ServerData();
print_r($zm);

I zobacz co tam tak naprawdę siedzi - będziesz wiedział wówczas jak sie do tego dobrać.
Analogicznie reszta

ps:
Aaaa.... no przecież jak wół masz:
return array(
'hostname' => $hostname,
'players' => $players,
'maxplayers' => $maxplayers,
'passworded' => (bool)$passworded
);
znaczy że masz tam tablice. Nie możesz tablicy wstawić jako tekstu. Musisz pokolei każdą wartość wstawiać


  1. $zm = $q->ServerData();
  2. $status = "Info: ".$zm['hostname']." <br />Zasady: ".$q->ServerRules()." <br />Graczy: ".$q->Players()." <br />Ping: ".$q->Ping();

analogicznie reszta
Gość
Info: 1
Zasady: 1
Graczy: 1
Ping: 1


przy kodzie (patrz dół):
  1. class IVMPQuery
  2. {
  3. var $socket = false;
  4. var $ip = false;
  5. var $port = false;
  6.  
  7. function Query($ip,$port,&$errno,&$errstr,$timeout = 5,$gettimeout = 1)
  8. {
  9. $this->Close();
  10. $this->ip = $ip;
  11. $this->port = $port;
  12. $this->socket = @fsockopen('udp://'.$ip,$port,$errno,$errstr,$timeout);
  13. if($this->socket === false) return false;
  14. @stream_set_timeout($this->socket,$gettimeout);
  15. return true;
  16. }
  17.  
  18. function Close()
  19. {
  20. if($this->socket !== false)
  21. {
  22. fclose($this->socket);
  23. $this->socket = false;
  24. }
  25. }
  26.  
  27. function GetPacketData($command)
  28. {
  29. $packet = 'IVMP';
  30. $packet .= $command;
  31. return $packet;
  32. }
  33.  
  34. function ServerData()
  35. {
  36. fputs($this->socket,$this->GetPacketData('i'));
  37. @fread($this->socket,5); // IVMPi
  38.  
  39. $len = ord(@fread($this->socket,4));
  40. $hostname = @fread($this->socket,$len); // read hostname
  41. $players = ord(@fread($this->socket,4)); // read players
  42. $maxplayers = ord(@fread($this->socket,4)); // read max players
  43. $passworded = ord(@fread($this->socket,1)); // 1 byte for password
  44.  
  45. return array(
  46. 'hostname' => $hostname,
  47. 'players' => $players,
  48. 'maxplayers' => $maxplayers,
  49. 'passworded' => (bool)$passworded
  50. );
  51. }
  52.  
  53. function ServerRules()
  54. {
  55. fputs($this->socket,$this->GetPacketData('r'));
  56. @fread($this->socket,5); // IVMPr
  57.  
  58. $count = ord(@fread($this->socket,4));
  59. $arr = array();
  60. for($i = 0; $i < $count; $i++)
  61. {
  62. $len = ord(@fread($this->socket,4));
  63. $key = @fread($this->socket,$len);
  64. $len = ord(@fread($this->socket,4));
  65. $arr[$key] = @fread($this->socket,$len);
  66. }
  67.  
  68. return $arr;
  69. }
  70.  
  71. function Players()
  72. {
  73. fputs($this->socket,$this->GetPacketData('l'));
  74. @fread($this->socket,5); // IVMPl
  75.  
  76. $count = ord(@fread($this->socket,4));
  77. $arr = array();
  78. for($i = 0; $i < $count; $i++)
  79. {
  80. $id = ord(@fread($this->socket,4));
  81. $len = ord(@fread($this->socket,4));
  82. $arr[$id] = @fread($this->socket,$len);
  83. }
  84.  
  85. return $arr;
  86. }
  87.  
  88. // Default return is ms int. If $asfloat == true, will return as seconds and as a float
  89. function Ping($asfloat = false)
  90. {
  91. $time = microtime(true);
  92. fputs($this->socket,$this->GetPacketData('p'));
  93. @fread($this->socket,5); // IVMPp
  94. $reply = @fread($this->socket,4);
  95. $time = microtime(true) - $time;
  96. if(!$asfloat)
  97. {
  98. $time *= 1000;
  99. $time = (int)round($time);
  100. }
  101. if($reply == 'PONG')
  102. {
  103. return $time;
  104. }
  105.  
  106. return false;
  107. }
  108. }
  109.  
  110. $q = new IVMPQuery;
  111.  
  112. if(!$q->Query('80.82.23.2',9999,$errno,$errstr,2))
  113. {
  114. $status = "brak łączności";
  115. }
  116. else
  117. {
  118. $info = $q->ServerData();
  119. $zasady = $q->ServerRules();
  120. $gracze = $q->Players();
  121. $lagi = $q->Ping();
  122. $status = "Info: ".print_r($info)." <br />Zasady: ".print_r($zasady)." <br />Graczy: ".print_r($gracze)." <br />Ping: ".print_r($lagi);
  123. }
nospor
Jakie print_r ? No przecież ci wyraźnie napisałem jak masz mieć....


nie: ".print_r($info)."
a:".$info['hostname']."


print_r($info); to sobie możesz dać do sprawdzenia co masz w $info a nie do wkładania do tekstu
Gość
Możesz sformatować ten kod, by był poprawny według twojej ostatniej wskazówki do wkładania do tekstu?
nospor
Przecież ci napisałem....

$status = "Info: ".$info['hostname']." <br />Zasady: ".print_r($zasady)." <br />Graczy: ".print_r($gracze)." <br />Ping: ".print_r($lagi);
resztę popraw sobie sam.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.