Jump to content

Get server info on PHP


lime|sg

Recommended Posts

Hello, community! I searched on the forums how to get server information using PHP, but I get the error "Data is invalid" using following code:

<?php
class CSocketMTA {
	function CSocketMTA( $IP, $PORT = 22003 ) {
		if ( $socket = fsockopen( "udp://$IP", $PORT + 123, $errno, $errstr ) ) {
			stream_set_timeout( $socket, 1, 0 );
			fwrite( $socket, "s" );
			$data = fread( $socket, 16384 );
			fclose( $socket );
			if ( substr( $data, 0, 4 ) == "EYE1" ) {
				$data = substr( $data, 4 );
				for ( $i = 0; $data != ""; $i++ ) {
					if ( substr( $data, 0, 2 ) == chr( 1 ) . "?" )
						$data = substr( $data, 2 );
					$l = ord( substr( $data, 0, 1 ) );
					$buffer[ $i ] = substr( $data, 1, $l - 1 );
					$data = substr( $data, $l );
				}
				$this->gameshort = $buffer[ 0 ];
				$this->port = $buffer[ 1 ];
				$this->name = $buffer[ 2 ];
				$this->rules[ 'game' ] = $buffer[ 3 ];
				$this->map = $buffer[ 4 ];
				$this->rules[ 'version' ]   = $buffer[ 5 ];
				$this->public = $buffer[ 6 ] != "0";
				$this->players = $buffer[ 7 ];
				$this->maxplayers = $buffer[ 8 ];
				$j = 0;
				for ( $i = 11; $i < sizeof( $buffer ) - 2; $i += 5 ) {
					$this->player[ $j ]->name   = $buffer[ $i ];
					$this->player[ $j ]->score  = $buffer[ $i + 3 ];
					$this->player[ $j ]->time   = 0;
					$j++;
				}
			}
			else echo "Data is invalid\n"; 
		}
		else echo "Can not open socket $IP:$PORT - $errstr\n"; 
	}
};
?>

I want get the information from any MTA:SA server. Help me, please! Thanks.

Link to post

Use this SDK for complex requests: https://wiki.multitheftauto.com/wiki/PHP_SDK

 

 And here is your fixed code, for request basic server informations:

<?php 

class CSocketMTA {
    function CSocketMTA($IP, $PORT = 22003) { 
        if ($socket = fsockopen("udp://$IP", $PORT + 123, $errno, $errstr)) {
            stream_set_timeout($socket, 1, 0);
            fwrite($socket, "s");
			
            $data = fread($socket, 16384);
            fclose($socket);  
             
            if (substr($data, 0, 4) == "EYE1") {
               $data = substr($data, 4);
				
               for ($i = 0; $data != ""; $i++) {  
                    if (substr($data, 0, 2) == chr(1)."?") $data = substr($data, 2);
					
                    $l				= ord(substr($data, 0, 1));
                    $buffer[$i]		= substr($data, 1, $l - 1);
                    $data			= substr($data, $l);
               }
				
                $this->ip					= $IP;
                $this->gameshort            = $buffer[0];
                $this->port                 = $buffer[1];
                $this->name                 = $buffer[2];
                $this->rules['game']		= $buffer[3];
                $this->map                  = $buffer[4];
                $this->rules['version']		= $buffer[5];
                $this->public               = $buffer[6] != "0";
                $this->players              = $buffer[7];
                $this->maxplayers           = $buffer[8];
				
                $j = 0;
                for ($i = 11; $i < sizeof($buffer) - 2; $i += 5) {
                    $this->player[$j]->name		= $buffer[$i];
                    $this->player[$j]->score	= $buffer[$i+3];
                    $this->player[$j]->time		= 0;
                    $j++;
                }
            } else echo "Data is invalid\n";
           } else echo "Can not open socket $IP:$PORT - $errstr\n";
    }
};

$MTA = new CSocketMTA("dayz.skycore.hu", 22003);

echo "Name: " . $MTA->name . "<br>";
echo "IP: " . $MTA->ip . "<br>";
echo "Players: " . $MTA->players."/".$MTA->maxplayers . "<br>";

 

Edited by stPatrick
Link to post

Nothing changed. Data is invalid. I think the problem in line 12.

1) What is "EYE1" means? 
2) Why substr($data, 0, 4isn't equals "EYE1" on my website?

PS: I discovered that $data is empty string. What is the problem? My PHP version on website is 7.1.21, I think there should be no problems... but empty string.

Edited by lime|sg
Link to post
39 minutes ago, lime|sg said:

Nothing changed. Data is invalid. I think the problem in line 12.

1) What is "EYE1" means? 
2) Why substr($data, 0, 4isn't equals "EYE1" on my website?

PS: I discovered that $data is empty string. What is the problem? My PHP version on website is 7.1.21, I think there should be no problems... but empty string.

I don't know what is EYE1 but this code works with my server, so I think the problem is in your server settings.

Check your $data variable value. Whats is in it?

For me: EYE1mta22003(SkyDayZ | MTA DayZ Europe [HUN|ENG|GER]SkyDayZNone1.50060

 

Whats your MTA server ip and port?

Edited by stPatrick
Link to post
Just now, lime|sg said:

<?php
    $MTA = new CSocketMTA("dayz.skycore.hu", 22003);
    echo $MTA->port;
?>

 

This is my server... the code don't work with that?

Because it's works for me.

Link to post

I have enabled PHP errors and got the next error:

Notice: fwrite(): send of 1 bytes failed with errno=1 Operation not permitted in .../httpdocs/tset/tset.php on line 6

The line is: fwrite($socket, "s");

Edited by lime|sg
Link to post
23 minutes ago, lime|sg said:

I have enabled PHP errors and got the next error:


Notice: fwrite(): send of 1 bytes failed with errno=1 Operation not permitted in .../httpdocs/tset/tset.php on line 6

The line is: fwrite($socket, "s");

I think your hosting disabled it for security reasons. Contact their support.

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...