Jeg har et script her:
<?php
$serverip = "81.7.129.73:27015";
$serverspy = & new serverspy();
$connected = $serverspy->connect($serverip);
if ($connected) {
$output = $serverspy->info();
print('Name: ' . $output["server_name"] . ' Players: ' . $output["players"] . '/' . $output["max_players"] . ' Map: ' . $output["server_map"]);
$serverspy->disconnect();
} else {
print('Error while communicating with the server.');
$serverspy->disconnect();
}
// Classes
class parse {
function getchar(&$data) {
return sprintf("%c", parse::getbyte($data));
}
function getbyte(&$data) {
if(empty($data)) return "";
$result = substr($data, 0, 1);
$data = substr($data, 1);
return ord($result);
}
function getint16(&$data) {
if(empty($data)) return "";
$lower = parse::getbyte($data);
$higher = parse::getbyte($data);
return ($higher << 8) | $lower;
}
function getint32(&$data) {
if(empty($data)) return "";
$lower = parse::getint16($data);
$higher = parse::getint16($data);
return ($higher << 16) | $lower;
}
function getfloat32(&$data) {
if(empty($data)) return "";
$result = unpack("ffloat", $data);
$data = substr($data, 4);
return $result['float'];
}
function getstring(&$data) {
if(empty($data)) return "";
$terminated = strpos($data, chr(0));
$result = substr($data, 0, $terminated);
$data = substr($data, $terminated + 1);
return htmlentities($result, ENT_COMPAT, "UTF-8");
}
}
class socket {
var $host;
var $port;
var $handle;
function connect($host, $protocol="udp") {
if(isset($this->handle)) {
$this->disconnect();
}
list($this->host, $this->port) = explode(":", $host);
$this->handle = @fsockopen($protocol."://".gethostbyname($this->host), $this->port, &$errno, &$errstr, $this->timeout);
if(!$this->handle) {
return FALSE;
}
if(function_exists("stream_set_timeout")) {
stream_set_timeout($this->handle, $this->timeout);
} else {
socket_set_timeout($this->handle, $this->timeout);
}
return TRUE;
}
function disconnect() {
fclose($this->handle);
}
function unread_bytes() {
$status = socket_get_status($this->handle);
return $status['unread_bytes'];
}
function send($command) {
fwrite($this->handle, $command, strlen($command));
}
function read($length=NULL) {
if(!is_null($length)) {
return fread($this->handle, $length);
}
$buffer = "";
do {
$buffer .= fread($this->handle, 1);
}
while($this->unread_bytes());
return $buffer;
}
}
define("A2S_SERVERQUERY_GETCHALLENGE", "W"); // request challenge
define("A2S_INFO", "TSource Engine Query\\0"); // server info request
define("A2S_PLAYER", "U"); // request player list
define("A2S_RULES", "V"); // request rules list
define("S2C_CHALLENGE", "A"); // challenge response (HL2)
define("S2A_INFO", "I"); // info response (HL2)
define("S2A_INFO_DETAILED", "m"); // info response (HL1)
define("S2A_PLAYER", "D"); // player response
define("S2A_RULES", "E"); // rules response
class serverspy extends socket {
var $timeout; // time out for 'socket'
var $challenge = -1;
function serverspy($timeout=1) {
$this->timeout = $timeout;
}
function communicate($command) {
$command = pack("N", 0xFFFFFFFF).$command;
$this->send($command); // function 'send' from 'socket'
$packets = array();
do {
$cache = $this->read(); // function 'read' from 'socket'
if(empty($cache)) {// if 'cache' is empty, try it again
$this->send($command); // function 'send' from 'socket'
$total_packets = 1;
continue;
}
// 4 byte packet header
$header = unpack("Nint", substr($cache, 0, 4));
$packet_type = sprintf("%u", $header['int']);
if($packet_type == 0xFFFFFFFF) {// single packet
$packets[0] = $cache;
$total_packets = 0;
} elseif ($packet_type == 0xFEFFFFFF) {// multi packets
$cache = substr($cache, 4); // strip off the 4 byte packet header
// byte 9 tells us two things. the low 4 bits holds the total number of packets sent
// by the server and the high 4 bits holds the index of the packet we just received.
$packet_info = unpack("Cbyte", substr($cache, 4, 1));
// total number of packets
if(!isset($total_packets)) $total_packets = $packet_info['byte'] & 0x0F;
$total_packets--;
// index of received packet
$index = (int)($packet_info['byte'] >> 4);
$packets[$index] = substr($cache, 5);
}
}
while($total_packets);
ksort($packets);
return substr(implode("", $packets), 4);
}
function getchallenge() {
$response = $this->communicate(A2S_SERVERQUERY_GETCHALLENGE);
$control_byte = parse::getchar($response);
if($control_byte != S2C_CHALLENGE) {
return FALSE;
}
$this->challenge = $response;
return TRUE;
}
function info() {
if(!$this->getchallenge()) { // moved up here from inside the HL2 query
return FALSE;
}
$response = $this->communicate(A2S_INFO);
$control_byte = parse::getchar($response);
if($control_byte != S2A_INFO && $control_byte != S2A_INFO_DETAILED) {
return FALSE;
}
$result = array();
if($control_byte == S2A_INFO) { // HL2 server
$result['protocol_version'] = parse::getbyte($response);
$result['server_name'] = parse::getstring($response);
$result['server_map'] = parse::getstring($response);
$result['game_directory'] = parse::getstring($response);
$result['game_description'] = parse::getstring($response);
$result['application_id'] = parse::getint16($response);
$result['players'] = parse::getbyte($response);
$result['max_players'] = parse::getbyte($response);
$result['bot_players'] = parse::getbyte($response);
$result['dedicated'] = parse::getchar($response);
$result['operating_system'] = parse::getchar($response);
$result['password'] = parse::getbyte($response);
$result['secure'] = parse::getbyte($response);
$result['server_version'] = parse::getstring($response);
} elseif($control_byte == S2A_INFO_DETAILED) { // HL1 server
$result['server_address'] = parse::getstring($response);
$result['server_name'] = parse::getstring($response);
$result['server_map'] = parse::getstring($response);
$result['game_directory'] = parse::getstring($response);
$result['game_description'] = parse::getstring($response);
$result['players'] = parse::getbyte($response);
$result['max_players'] = parse::getbyte($response);
$result['protocol_version'] = parse::getbyte($response);
$result['dedicated'] = parse::getchar($response);
$result['operating_system'] = parse::getchar($response);
$result['password'] = parse::getbyte($response);
$result['modded'] = parse::getbyte($response);
if($result['modded']) {
$result['mod_website'] = parse::getstring($response);
$result['mod_download_server'] = parse::getstring($response);
$result['mod_unused'] = parse::getstring($response);
$result['mod_version'] = parse::getint32($response);
$result['mod_size'] = parse::getint32($response);
$result['mod_serverside_only'] = parse::getbyte($response);
$result['mod_custom_dll'] = parse::getbyte($response);
}
$result['secure'] = parse::getbyte($response);
$result['bot_players'] = parse::getbyte($response);
}
return $result;
}
function player() {
$response = $this->communicate(A2S_PLAYER.$this->challenge);
$control_byte = parse::getchar($response);
if($control_byte != S2A_PLAYER) {
return FALSE;
}
$result = array();
$count = parse::getbyte($response);
for($i = 0; $i < $count; $i++) {
if(empty($response)) continue;
$index = parse::getbyte($response);
$name = parse::getstring($response);
$frags = parse::getint32($response);
$time = parse::getfloat32($response);
$result[] = array(
"index" => $index,
"name" => $name,
"frags" => $frags,
"time" => (($time != -1) ? (date("H:i:s", mktime(0, 0, $time))) : ("BOT"))
);
}
return $result;
}
}
?>
Men jeg får 500 Internal server error. Hvorfor?