SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Tools and Files

Reply
 
Thread Tools Display Modes
Old 30/06/2012, 02:52 PM   #1
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 155
Reputation: 40
Default [PHP] Query and RCON API (24/07/2014)

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

Last edited by StatusRed; 24/07/2014 at 12:10 PM. Reason: New version (1.1)
StatusRed is offline   Reply With Quote
Old 30/06/2012, 02:56 PM   #2
next-studio|TheKiller
Guest
 
Posts: n/a
Default Re: [PHP] Query and RCON API (30/06/2012)

Good job again!
Alot of more functions than the westie's query api ^_^
  Reply With Quote
Old 30/06/2012, 03:00 PM   #3
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 155
Reputation: 40
Default Re: [PHP] Query and RCON API (30/06/2012)

Quote:
Originally Posted by next-studio|TheKiller View Post
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.
StatusRed is offline   Reply With Quote
Old 10/07/2012, 10:03 AM   #4
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 479
Reputation: 21
Default Re: [PHP] Query and RCON API (30/06/2012)

How can you get the information detailed on the page like in a table?
SomebodyAndMe is offline   Reply With Quote
Old 10/07/2012, 04:20 PM   #5
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 155
Reputation: 40
Default Re: [PHP] Query and RCON API (30/06/2012)

Quote:
Originally Posted by SomebodyAndMe View Post
How can you get the information detailed on the page like in a table?
Try something like:

PHP Code:
<?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.";
}
?>

Last edited by StatusRed; 10/07/2012 at 05:02 PM.
StatusRed is offline   Reply With Quote
Old 10/07/2012, 04:53 PM   #6
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 479
Reputation: 21
Default Re: [PHP] Query and RCON API (30/06/2012)

Code:
Parse error: syntax error, unexpected T_AS, expecting ';' in /home/kevin/public_html/online.php on line 60
PHP Code:
<?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.
SomebodyAndMe is offline   Reply With Quote
Old 10/07/2012, 05:01 PM   #7
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 155
Reputation: 40
Default Re: [PHP] Query and RCON API (30/06/2012)

Quote:
Originally Posted by SomebodyAndMe View Post
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?
StatusRed is offline   Reply With Quote
Old 10/07/2012, 05:08 PM   #8
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 479
Reputation: 21
Default Re: [PHP] Query and RCON API (30/06/2012)

Yeah just working out the css now, http://transrp.tk/online.php
SomebodyAndMe is offline   Reply With Quote
Old 10/07/2012, 05:11 PM   #9
StatusRed
Big Clucker
 
Join Date: Jan 2011
Location: UK
Posts: 155
Reputation: 40
Default Re: [PHP] Query and RCON API (30/06/2012)

Quote:
Originally Posted by SomebodyAndMe View Post
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!
StatusRed is offline   Reply With Quote
Old 10/07/2012, 05:14 PM   #10
SomebodyAndMe
Huge Clucker
 
SomebodyAndMe's Avatar
 
Join Date: Feb 2010
Location: 52.511938,6.092434 find it.
Posts: 479
Reputation: 21
Default Re: [PHP] Query and RCON API (30/06/2012)

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 Code:
<?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.googleapis.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>
SomebodyAndMe is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tool/Web/Other] [Java] Query and RCON API (25/06/2012) StatusRed Tools and Files 10 02/02/2013 10:44 AM
[Tool/Web/Other] [Python] Query and RCON API (23/06/2012) StatusRed Tools and Files 13 25/06/2012 03:27 PM
[Android]SA-MP App - Query and send RCON commands to your servers R@f Everything and Nothing 25 04/06/2012 11:35 PM
SAMP RCON & Query API CaTaLinU Scripting Help 5 11/02/2012 12:25 PM
Westies API / SAMP Api (Query & RCON) Lee Strong Help Archive 2 14/10/2010 09:37 AM


All times are GMT. The time now is 07:54 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.