LoginSignup
10
7

More than 5 years have passed since last update.

minecraft のServerのステータスをコマンドラインのPHPで取得する

Posted at

h5EzsgF.png


普段サーバー上のtmuxでminecraftサーバーを監視してるんですが、
なんかサクッとサーバーの状態を取得できないかなーって思い、
調べたらQueryってのがあることと、PHPで取得できるってことが
わかったので、備忘録的にしておこうかと。

下地

  • server
    • Ubuntu server 14.04 LTS
    • Minecraft 1.9.2
  • client(サブPC)
    • kona Linux 3.0
    • PHP 5.6.19

1. PHPってコマンドラインで動かせる!?

実を言うと、筆者はコマンドライン(shell)上で
PHPを動かしたことがなかったので

こちらのありがたい記事を読んでそういう使い方があるのかと知りました
PHPの導入については主題とズレるので別途お願い致します。

2.PHP-Minecraft-Queryをつかう

xPaw/PHP-Minecraft-Query | Github

探してみたらGithubでありました。
こちらのPHPスクリプトを使おうと思います。

git cloneでダウンロードします。

$ git clone https://github.com/xPaw/PHP-Minecraft-Query.git

3.さあ動かそう

移動

cd PHP-Minecraft-Query

移動しましたらサンプルコードを書きます

vi test.php

<?php
    require __DIR__ . '/src/MinecraftQuery.php';
    require __DIR__ . '/src/MinecraftQueryException.php';

    use xPaw\MinecraftQuery;
    use xPaw\MinecraftQueryException;

    $Query = new MinecraftQuery( );

    try
    {
        $Query->Connect( '<SERVER_ADDRESS>', 25565 );

        print_r( $Query->GetInfo( ) );
        print_r( $Query->GetPlayers( ) );
    }
    catch( MinecraftQueryException $e )
    {
        echo $e->getMessage( );
    }
?>

サーバーのアドレスのところを編集し、保存します。

いざ、実行

$ php test.php
Array
(
    [HostName] => A Minecraft Server
    [GameType] => SMP
    [GameName] => MINECRAFT
    [Version] => 1.9.2
    [Plugins] =>
    [Map] => world
    [Players] => 0
    [MaxPlayers] => 20
    [HostPort] => 25565
    [HostIp] => 127.0.1.1
    [Software] => Vanilla
)

成功すればこんな感じに表示されるはずです。

おわりに

minecraftの状態をPHPでサクッと取得出来ちゃうのは良いですね。
これでcurlやMandrillなんかを使って通知とかにも使えそうです。
IFTTTのWebhookを利用した連携なんかも楽しそうです。
成功次第Qiitaに書けたらなと思います。
自分への理解はアウトプットした時のほうが理解できるので。個人的に。
話が逸れました。
とても簡単にできるのでMinecraftサーバーの管理者さんは
試してみると良いのでは、それでは。

10
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
7