[Tool/Web/Other] [PHP] Query and RCON API (30/06/2012)
#1

SA-MP Query and RCON API for PHP
Edward McKnight (EM-Creations.co.uk) - StatusRed
Version: 1.1 (24/07/2014)
PHP Version: 5.3 (should work with earlier versions also however)
Additional credits: Westie (for the original PHP SA-MP API and inspiration for this API)

As well as the examples here; included in the download are SampRconExample.php and SampQueryExample.php files. These are full of examples of how to call functions as well as documentation.

Feel free to post if you have any questions or problems.

SampQuery functions:
SampQuery(server, port)
SampQuery->connect()
SampQuery->close()
SampQuery->getInfo()
SampQuery->getBasicPlayers()
SampQuery->getDetailedPlayers()
SampQuery->getRules()
SampQuery->getPing() - Not in Westie's API

Example | Output server info and basic players:
PHP код:
<?php
/**
 *
 * @author Edward McKnight (EM-Creations.co.uk)
 * This example assumes this file is in the same directory as the SampQuery class.
 */
        
require("SampQuery.class.php");
        
$query = new SampQuery("127.0.0.1"7777);
        
        if (
$query->connect()) { // If a successful connection has been made
            
            
print_r($query->getInfo()); // Print server info array
            
            
print_r($query->getBasicPlayers()); // Print basic players array, connection will time out if the player counter is above 100 and will return an empty array if no players are online
            
$query->close(); // Close the connection
        
} else {
            echo 
"Server did not respond!";
        }
?>
SampRcon functions:
SampRcon(server, port, password)
SampRcon->connect()
SampRcon->close()
SampRcon->getCommandList()
SampRcon->getServerVariables()
SampRcon->setWeather(weatherID=1)
SampRcon->setGravity(gravity=0.008)
SampRcon->ban(playerID)
SampRcon->kick(playerID)
SampRcon->banAddress(address)
SampRcon->unbanAddress(address)
SampRcon->reloadLog()
SampRcon->reloadBans()
SampRcon->say(message)
SampRcon->changeGameMode(gameMode)
SampRcon->setGameModeText(gameModeText) - Not in Westie's API
SampRcon->nextGameMode()
SampRcon->gmx() {same as SampRcon->nextGameMode()}
SampRcon->execConfig(config)
SampRcon->loadFilterscript(fs)
SampRcon->loadFS(fs) {same as SampRcon->loadFilterscript(fs)}
SampRcon->unloadFilterscript(fs)
SampRcon->unloadFS(fs) {same as SampRcon->unloadFilterscript(fs)}
SampRcon->reloadFilterscript(fs)
SampRcon->reloadFS(fs) {same as SampRcon->reloadFilterscript(fs)}
SampRcon->exitGame()
SampRcon->setHostName(hostName) - Not in Westie's API
SampRcon->setMapName(mapName) - Not in Westie's API
SampRcon->setTime(time) - Not in Westie's API
SampRcon->setURL(url) - Not in Westie's API
SampRcon->setPassword(password) - Not in Westie's API
SampRcon->removePassword() - Not in Westie's API
SampRcon->setRconPassword(password) - Not in Westie's API
SampRcon->disableRcon() - Not in Westie's API
SampRcon->enableQuery() - Not in Westie's API
SampRcon->disableQuery() - Not in Westie's API
SampRcon->enableAnnounce() - Not in Westie's API
SampRcon->disableAnnounce() - Not in Westie's API
SampRcon->setMaxNPCs(maxNPCs) - Not in Westie's API
SampRcon->call(command, delay)

Example | Output command list:
PHP код:
<?php
/**
 *
 * @author Edward McKnight (EM-Creations.co.uk)
 * This example assumes this file is in the same director as the SampRcon class.
 */
        
require("SampRcon.class.php");
        
$query = new SampRcon("127.0.0.1"7777"changeme1");
        
        if (
$query->connect()) { // If a successful connection has been made
            // Output command list
            
print_r($query->getCommandList());
            
            
$query->close(); // Close the connection
        
} else {
            echo 
"Server did not respond!";
        }
        
$query->close(); // Close the connection
?>
Example | Ban player:
PHP код:
<?php
/**
 *
 * @author Edward McKnight (EM-Creations.co.uk)
 * This example assumes this file is in the same directory as the SampRcon class.
 */
        
require("SampRcon.class.php");
        
$query = new SampRcon("127.0.0.1"7777"changeme1");
        
        if (
$query->connect()) { // If a successful connection has been made
            
$query->ban(1); // Ban player ID 1
            
$query->reloadBans(); // Reload the server's bans file
            
$query->close(); // Close the connection
        
} else {
            echo 
"Server did not respond!";
        }
        
$query->close(); // Close the connection
?>
Reply
#2

Good job again!
Alot of more functions than the westie's query api ^_^
Reply
#3

Quote:
Originally Posted by next-studio|TheKiller
Посмотреть сообщение
Good job again!
Alot of more functions than the westie's query api ^_^
Yeah, although much of it was doable in Westie's API by using the "Call" function. It definitely couldn't retrieve the ping of the server though.
Reply
#4

How can you get the information detailed on the page like in a table?
Reply
#5

Quote:
Originally Posted by SomebodyAndMe
Посмотреть сообщение
How can you get the information detailed on the page like in a table?
Try something like:

PHP код:
<?php require("SampQuery.class.php");
$query = new SampQuery("127.0.0.1"7777);
if (
$query->connect()) {
   echo 
"<table><tr><td><strong>ID</strong></td><td><strong>Name</strong></td><td><strong>Score</strong></td><td><strong>Ping</strong></td></tr>";
   
   
$players $query->getDetailedPlayers();
   foreach (
$players as $player) {
      echo 
"<table><tr><td>{$player['playerid']}</td><td>{$player['nickname']}</td><td>{$player['score']}</td><td>{$player['ping']}</td></tr>";
   }
   echo 
"</table>";
} else {
   echo 
"Error: Couldn't connect to server.";
}
?>
Reply
#6

Код:
Parse error: syntax error, unexpected T_AS, expecting ';' in /home/kevin/public_html/online.php on line 60
PHP код:
<?php
$query 
= new SampQuery("178.32.227.161"7777); 
if (
$query->connect()) { 
   echo 
"<table><tr><td><strong>ID</strong></td><td><strong>Name</strong></td><td><strong>Score</strong></td><td><strong>Ping</strong></td></tr>"
    
   
$players $query->getDetailedPlayers(); 
   foreach (
$players as $player) { //line 60.
      
echo "<table><tr><td>{$player['playerid']}</td><td>{$player['nickname']}</td><td>{$player['score']}</td><td>{$player['ping']}</td></tr>"
   } 
   echo 
"</table>"
} else { 
   echo 
"Error: Couldn't connect to server."

?>
Changed 'for' to foreach.
Reply
#7

Quote:
Originally Posted by SomebodyAndMe
Посмотреть сообщение
Changed 'for' to foreach.
Yeah, that's what I meant sorry; didn't test it, just wrote it off the top of my head. Apart from that does it work?
Reply
#8

Yeah just working out the css now, http://transrp.tk/online.php
Reply
#9

Quote:
Originally Posted by SomebodyAndMe
Посмотреть сообщение
Yeah just working out the css now, http://transrp.tk/online.php
Nice work; it's great to see someone using my API. Good luck with your project!
Reply
#10

Maybe you can help me, I'm trying to align it to the left as you may see how it's bugged atm.

Code now:

PHP код:
<?php session_start(); ?>
<?php 
   
include("SampQuery.class.php"); // Require or include the SampQuery class file     
?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<link type="text/css" href="menu.css" rel="stylesheet" />
<link href='http://fonts.******apis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="menu.js"></script>
<meta charset="utf-8">
<title>Transporter Roleplay</title>
<style type="text/css">
table, td, th
{
border:0px solid grey;
text-align:left;
border-collapse: separate;
border-spacing: 2px;
white-space: normal;
line-height: normal;
font-weight: normal;
font-size: medium;
font-variant: normal;
font-style: normal;
color: -webkit-text;
text-align: -webkit-auto;
}
th
{
background-color:green;
color:white;
text-align:left;
}
tr {
display: table-row;
vertical-align: inherit;
border-color: inherit;
}
tabel {
font-size:5px;
font-family:"Times New Roman", Times, serif;
}

A:link {text-decoration: bold; color: black}
A:visited {text-decoration: bold; color: black}
A:active {text-decoration: bold; color: black}
A:hover {text-decoration: underline; color: black;}
</style>

</head>

<body>
<a href="http://apycom.com/" style="visibility:hidden"></a>
<br />
<div id="wrapper">
<div id="header">
</div>
<div id="nav">
    <div id="menu">
        <ul class="menu">
            <li><a href="/index.php" class="parent"><span>Home</span></a></li>
            <li><a href="forums"><span>Forums</span></a></li>
            <li><a href="#"><span>Server Team</span></a></li>
            <li><a href="online.php"><span>Community</span></a></li>
            <li><a href="#"><span>Patrocinations</span></a></li>
            <li><a href="#"><span>Donations</span></a></li>
            <?php if(empty($_SESSION['myusername'])){ ?>
            <li><a href="login.php"><span>Login</span></a></li>
            <?php }else{ ?>
            <li><a href="user.php?user=<?php echo $_SESSION['myusername']; ?>"><span><?php echo $_SESSION['myusername']; ?></span></a>
            <div><ul>
            <?php if($_SESSION['playerlevel']>4){ ?>
            <li><a href="adminpanel" class="parent"><span>Admin Panel</span></a>
            </li>
            <?php ?>
            <li><a href="logout.php" class="parent"><span>Log out</span></a>            
            </ul></div></li>
            <?php ?>
        </ul>
    </div>
    </div>
    <div id="mainContent">
    <h1 style="font-family: 'Open Sans', sans-serif;">Welcome</h1><br />
    <p style="font-family: 'Open Sans', sans-serif;">
<?php

$query 
= new SampQuery("178.32.227.161"7777); 

if (
$query->connect()) { 
   echo 
"<table width='100%' border='1' cellpadding='3' cellspacing='1' bgcolor='#FFFFFF'><tr><th><strong>ID</strong></th><th><strong>Name</strong></th><th><strong>Score</strong></th><th><strong>Ping</strong></th></tr>"
    

   
$players $query->getDetailedPlayers(); 
   foreach (
$players as $player) { 
      echo 
"<table width='100%' border='1' cellpadding='3' cellspacing='1' bgcolor='#FFFFFF'><tr><td>{$player['playerid']}</td><td><A HREF='http://transrp.tk/user.php?user={$player['nickname']}'>{$player['nickname']}</a></td><td>{$player['score']}</td><td>{$player['ping']}</td></tr>"
   } 

   echo 
"</table></table>"
} else { 
   echo 
"Error: Couldn't connect to server."

?>
        </p>
</div>
<div id="footer">
</div>
</div>
</body>
</html>
Reply
#11

Nice work.
Reply
#12

Quote:
Originally Posted by iRaiDeN
View Post
Nice work.
Thanks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)