lime|sg Posted December 6, 2019 Share Posted December 6, 2019 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 comment
Moderators Patrick Posted December 6, 2019 Moderators Share Posted December 6, 2019 (edited) 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 December 6, 2019 by stPatrick Link to comment
lime|sg Posted December 6, 2019 Author Share Posted December 6, 2019 (edited) Nothing changed. Data is invalid. I think the problem in line 12. 1) What is "EYE1" means? 2) Why substr($data, 0, 4) isn'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 December 6, 2019 by lime|sg Link to comment
Moderators Patrick Posted December 6, 2019 Moderators Share Posted December 6, 2019 (edited) 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, 4) isn'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 December 6, 2019 by stPatrick Link to comment
lime|sg Posted December 6, 2019 Author Share Posted December 6, 2019 Nothing outputs. Empty string, empty variable. Link to comment
Moderators Patrick Posted December 6, 2019 Moderators Share Posted December 6, 2019 2 minutes ago, lime|sg said: Nothing outputs. Empty string, empty variable. Whats the IP:PORT? Link to comment
lime|sg Posted December 6, 2019 Author Share Posted December 6, 2019 <?php $MTA = new CSocketMTA("dayz.skycore.hu", 22003); echo $MTA->port; ?> Link to comment
Moderators Patrick Posted December 6, 2019 Moderators Share Posted December 6, 2019 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 comment
lime|sg Posted December 6, 2019 Author Share Posted December 6, 2019 I copied your code and pasted in separated PHP file. Answer: Data is invalid Name: IP: Players: / Link to comment
Moderators Patrick Posted December 6, 2019 Moderators Share Posted December 6, 2019 10 minutes ago, lime|sg said: I copied your code and pasted in separated PHP file. Answer: Data is invalid Name: IP: Players: / I have no idea, but it's works: http://skycore.hu/test/index.php Something wrong in your webserver settings. Link to comment
lime|sg Posted December 6, 2019 Author Share Posted December 6, 2019 (edited) 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 December 6, 2019 by lime|sg Link to comment
Moderators Patrick Posted December 6, 2019 Moderators Share Posted December 6, 2019 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 comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now