Ixtabay

Member
Code:
<?php
/*
shard_status.php
v1.0

This is an example of how to get the status of a UO emulator (RunUO, ServUO, Sphere, etc.)
by sending a packet to the shard address:port.

\x7f\x00\x00\x01\xf1\x00\x04\xff

Newer ServUO Shards will return something like this:
Array ( [0] => ServUO [1] => Name=My Shard [2] => Age=3 [3] => Clients=1 [4] => Items=112187 [5] => Chars=2934 [6] => Mem=217514K [7] => Ver=2 )

RunUO and Sphere Shards don't return version:
Array ( [0] => ServUO [1] => Name=My Shard [2] => Age=3 [3] => Clients=1 [4] => Items=112187 [5] => Chars=2934 [6] => Mem=217514K )

2016 - Ixtabay for www.RunUO.com
*/


$shard_addr ="localhost";
$shard_port ="2593";

$fp = fsockopen($shard_addr, $shard_port);

if (!$fp) {
    echo "Cannot connect to $shard_addr:$shard_port\n";
} else {

    fwrite($fp, "\x7f\x00\x00\x01\xf1\x00\x04\xff");
    stream_set_timeout($fp, 2);
    $res = fread($fp, 2000);

    $info = stream_get_meta_data($fp);
    fclose($fp);

    if ($info['timed_out']) {
        echo 'Connection to to $shard_addr:$shard_port timed out!';
    } else {

	$ver = '';

	$arr = explode(',', $res);
// print_r($arr); // Uncomment if you want to see the raw array
	
$emu 	 = $arr[0];
$name 	 = ltrim(strstr($arr[1],'='), '=');
$age 	 = ltrim(strstr($arr[2], '='), '=');
$clients = ltrim(strstr($arr[3], '='), '=');
$items 	 = ltrim(strstr($arr[4], '='), '=');
$mobs 	 = ltrim(strstr($arr[5], '='), '=');
$mem 	 = ltrim(strstr($arr[6], '='), '=');

if ($emu <> "ServUO") $ver = "Unknown"; // Only ServUO Shards return version
else $ver = ltrim(strstr($arr[07], '='), '=');


// Example usage
echo <<<EOF
<style type="text/css">
table tr th,
table tr td {
    border-right: none;
    border-bottom: none;
    cell-padding-left: 2px;
	min-width: 100px;	
}
th {
	text-align:right;
}
</style>

<table>
	<tr>
		<th>Status:</th>
		<td>Online</td>
	</tr>
	<tr>
		<th>Emu:</th>
		<td>$emu</td>
	</tr>
	<tr>
		<th>Name:</th>
		<td>$name</td>
	</tr>
	<tr>
		<th>Uptime:</th>
		<td>$age hours</td>
	</tr>
	<tr>
		<th>Players Online:</th>
		<td>$clients</td>
	</tr>
	<tr>
		<th>Total Items:</th>
		<td>$items</td>
	</tr>
	<tr>
		<th>Mobs:</th>
		<td>$mobs</td>
	</tr>
	<tr>
		<th>Memory Used:</th>
		<td>$mem</td>
	</tr>	
	<tr>
		<th>Version:</th>
		<td>$ver</td>
	</tr>		
</table>

EOF;

		
		
    }

}



?>
 
Awesome work :D
Some PHP servers disallow the use of sockets in PHP, do you have a cURL alternative too?
You can only check to see if the port is open, but you won't be able to perform fread/fwrite type functions with curl.

So, if you just want to check up/down status, you could try something like this:

Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"localhost:2593");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "localhost\\r\
");
$data = curl_exec ($ch);
echo $data;
 

Active Shards

Donations

Total amount
$80.00
Goal
$1,000.00
Back