From 1caf4a6cd6b9a1c4a4ac07906c81e10d90c8a34c Mon Sep 17 00:00:00 2001 From: lolcat Date: Sat, 22 Jul 2023 14:43:50 -0400 Subject: [PATCH] done --- README.md | 7 +++++++ example.php | 21 +++++++++++++++++++++ mcpi.php | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 README.md create mode 100644 example.php create mode 100644 mcpi.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..14b6e9e --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# MCPI ping +A deadass simple library to ping Minecraft: Pi Edition serbers. + +For an example on how to use, view the example.php file!! + +### License +MIT diff --git a/example.php b/example.php new file mode 100644 index 0000000..2dc55b3 --- /dev/null +++ b/example.php @@ -0,0 +1,21 @@ +ping("androidmc.hopto.org", 19133)); +}catch(Exception $e){} + +try{ + print_r($mcpi->ping("68.194.116.245", 19132)); +}catch(Exception $e){} + +try{ + print_r($mcpi->ping("146.198.132.171", 19132)); +}catch(Exception $e){} + +try{ + print_r($mcpi->ping("pi-land.minecraft.pe", 19132)); +}catch(Exception $e){} diff --git a/mcpi.php b/mcpi.php new file mode 100644 index 0000000..29b92df --- /dev/null +++ b/mcpi.php @@ -0,0 +1,53 @@ + $timeout, "usec" => 0]); + + // get string size + // for some reason, not using msg_peek makes the second + // socket_recv call hang + if(socket_recv($sock, $strlen, 36, MSG_PEEK | MSG_WAITALL) === false){ + + throw new Exception("Failed to receive datalen"); + } + + $strlen = unpack("c", substr($strlen, 34, 35))[1]; + + if(socket_recv($sock, $reply, (35 + $strlen), MSG_WAITALL) === false){ + + throw new Exception("Failed to receive string"); + } + + $reply = substr($reply, 35, (35 + $strlen)); + + $reply = explode(";", $reply); + + if(count($reply) !== 3){ + + throw new Exception("Received malformed string"); + } + + $reply = [ + "game" => $reply[0], + "type" => $reply[1], + "name" => $reply[2] + ]; + + return $reply; + } +}