Rownlin Posted June 7, 2012 Posted June 7, 2012 Hi, I want to query my MTA Server using PHP, and see if it's online, the number of player, etc. How can I do that ? I already tried GameQ (http://callofdutystats.net/) but it doesn't work (it says it's off but it's on..) Also, I can't use PHP SDK of MTA because i want to check other servers, so I can't edit their LUA scripts.. Thank you.
Scripting Moderators Sarrum Posted June 7, 2012 Scripting Moderators Posted June 7, 2012 http://gameq.sourceforge.net/ works fine. UDP port is open (on website)?
Rownlin Posted June 7, 2012 Author Posted June 7, 2012 Yes... Here's my code : require_once "GameQ/GameQ.php"; $query_api = new GameQ; $server["mtasa"] = array("mtasa","x.x.x.x","22003"); // Some IP I took on game-monitor $query_api->addServers($server); try { $datas = $query_api->requestData(); } catch (GameQ_Exception $e) { echo $e; exit(); } var_dump($datas); exit(); And return value : array(1) { ["mtasa"]=> array(5) { ["gq_online"]=> bool(false) ["gq_address"]=> string(13) "xx.xx.xx.xx" ["gq_port"]=> string(5) "22003" ["gq_prot"]=> string(3) "ase" ["gq_type"]=> string(5) "mtasa" } }
Scripting Moderators Sarrum Posted June 7, 2012 Scripting Moderators Posted June 7, 2012 require_once ('GameQ/GameQ.php'); //class $server['mtasa'] = array ('mtasa', '192.168.1.2', 22003+123); //ip & port $query = new GameQ; $query -> addServers ($server); $data = $query->requestData(); foreach ($server AS $server_id => $values) { $info = $data[$server_id]; if (!$info["servername"]) { echo 'Server offline'; } else { echo "Server name: "; echo $info["servername"]; echo '<br/>'; echo "Gamemode: "; echo $info["gametype"]; echo '<br/>'; echo "Map: "; echo $info["map"]; echo '<br/>'; echo "Players: "; echo $info["num_players"] ."/". $info["max_players"]; echo '<br/>'; if ($info["password"] == '0') { $pw = "No"; } else { $pw = "Yes"; } echo "Password: "; echo $pw; echo '<br/><br/>'; } }
Rownlin Posted June 7, 2012 Author Posted June 7, 2012 Mmh.. Why did you added 123 to the port ? But thank you, it works ! ^^
Scripting Moderators Sarrum Posted June 7, 2012 Scripting Moderators Posted June 7, 2012 One more thing, port+123 is how MTA was configured to communicate with both ASE and game-monitor servers for simplicity. It is not some magic formula.
Scripting Moderators Sarrum Posted June 8, 2012 Scripting Moderators Posted June 8, 2012 Okay, thank you My pleasure.
Stevenn Posted June 8, 2012 Posted June 8, 2012 I'm sorry for hi-jacking the topic but I seem to have a problem with my stats.php <?php require_once ('GameQ/GameQ.php'); //class $server['mtasa'] = array ('mtasa', 'serverIP', 22003+123); //ip & port $query = new GameQ; $query -> addServers ($server); $data = $query->requestData(); foreach ($server AS $server_id => $values) { $info = $data[$server_id]; if (!$info["servername"]) { echo 'Server offline'; } else { echo "Server name: "; echo $info["servername"]; echo '<br/>'; echo "Gamemode: "; echo $info["gametype"]; echo '<br/>'; echo "Map: "; echo $info["map"]; echo '<br/>'; echo "Players: "; echo $info["num_players"] ."/". $info["max_players"]; echo '<br/>'; if ($info["password"] == '0') { $pw = "No"; } else { $pw = "Yes"; } echo "Password: "; echo $pw; echo '<br/><br/>'; } ?> result: Parse error: syntax error, unexpected $end in /home/a6564823/public_html/php/stats.php on line 35
Scripting Moderators Sarrum Posted June 8, 2012 Scripting Moderators Posted June 8, 2012 result: Parse error: syntax error, unexpected $end in /home/a6564823/public_html/php/stats.php on line 35 add '}' on line 35
Anderl Posted July 3, 2012 Posted July 3, 2012 I know this is a bit outdated already but I just want to say something. Here: if ($info["password"] == '0') { $pw = "No"; } else { $pw = "Yes"; } You can simplify this to: echo $info['password'] == '0' ? 'No' : 'Yes'; Isn't it easier?
Aris Posted June 14, 2013 Posted June 14, 2013 <?php require_once ('GameQ/GameQ.php'); //class $server['mtasa'] = array ('mtasa', 'serverIP', 22003+123); //ip & port $query = new GameQ; $query -> addServers ($server); $data = $query->requestData(); foreach ($server AS $server_id => $values) { $info = $data[$server_id]; if (!$info["servername"]) { echo 'Server offline'; } else { echo "Server name: "; echo $info["servername"]; echo '<br/>'; echo "Gamemode: "; echo $info["gametype"]; echo '<br/>'; echo "Map: "; echo $info["map"]; echo '<br/>'; echo "Players: "; echo $info["num_players"] ."/". $info["max_players"]; echo '<br/>'; if ($info["password"] == '0') { $pw = "No"; } else { $pw = "Yes"; } echo "Password: "; echo $pw; echo '<br/><br/>'; } } ?> I want to here also shows the players who are online
Jaysds1 Posted June 16, 2013 Posted June 16, 2013 You already added that... echo $info["num_players"] ."/". $info["max_players"];
neves768 Posted July 19, 2013 Posted July 19, 2013 I tried to put a list of players, but could not. Someone here can put a list of players
Jaysds1 Posted July 21, 2013 Posted July 21, 2013 Do you mean showing all the players that's in the server? By Name?
LilDollaTechZone Posted July 28, 2013 Posted July 28, 2013 On SMF I am trying to use the GameQuery but I only get the 'Server offline' instead of the data. <?php require_once ('GameQ/GameQ.php'); //class $server['mtasa'] = array ('mtasa', '24.131.58.234', 22003+123); //ip & port $query = new GameQ; $query -> addServers ($server); $data = $query->requestData(); foreach ($server AS $server_id => $values) { $info = $data[$server_id]; if (!$info["servername"]) { echo 'Server Offline'; } else { echo "Server name: "; echo $info["servername"]; echo '<br/>'; echo "Players: "; echo $info["num_players"] ."/". $info["max_players"]; echo '<br/>'; if ($info["password"] == '0') { $pw = "No"; } else { $pw = "Yes"; } echo "Password: "; echo $pw; echo '<br/><br/>'; } } ?>
長瀞早瀬 Posted September 5, 2022 Posted September 5, 2022 (edited) Actual code (GameQ 1.12 Update): <?php require_once ''.$_SERVER[DOCUMENT_ROOT].'/engine/mta_sdk/GameQ.php'; $servers = array( 'server 1' => array('ase', '46.174.48.112',22003+123) ); $query = new GameQ; $query -> addServers ($servers); $query->setOption('timeout', 200); $data = $query->requestData(); function print_results($results) { foreach ($results as $id => $data) { printf("<h2>%s</h2>\n", $id); print_table($data); } } function print_table($data) { $gqs = array('gq_online', 'gq_address', 'gq_port', 'gq_prot', 'gq_type'); if (!$data['gq_online']) { printf("<p>The server did not respond within the specified time.</p>\n"); return; } print("<table><thead><tr><td>Variable</td><td>Value</td></tr></thead><tbody>\n"); foreach ($data as $key => $val) { if (is_array($val)) continue; $cls = empty($cls) ? ' class="uneven"' : ''; if (substr($key, 0, 3) == 'gq_') { $kcls = (in_array($key, $gqs)) ? 'always' : 'normalise'; $key = sprintf("<span class=\"key-%s\">%s</span>", $kcls, $key); } printf("<tr%s><td>%s</td><td>%s</td></tr>\n", $cls, $key, $val); } print("</tbody></table>\n"); } print_results($data); ?> Result: Edited September 5, 2022 by 長瀞早瀬
Recommended Posts