Jump to content

[PHP] Query System


Recommended Posts

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" } }
Link to comment
  • Scripting Moderators

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/>'; 
    } 
} 

:P

Link to comment
  • Scripting Moderators

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.

Link to comment

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
Link to comment
  • Scripting Moderators
result:
Parse error: syntax error, unexpected $end in /home/a6564823/public_html/php/stats.php on line 35

add '}' on line 35 :D

Link to comment
  • 4 weeks later...

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?

Link to comment
  • 11 months later...

<?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

Link to comment
  • 1 month later...

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/>'; 
        } 
    } 
?> 
  

Link to comment
  • 9 years later...

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:
bYieb-Z9VhI.jpg?size=896x600&quality=96&

Edited by 長瀞早瀬
Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...