HTTP request?

Discussion in 'Plugin Development' started by ryanshawty, Jan 26, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    ryanshawty

    Is there any way I can request e.g. play count through an HTTP request from PHP, I can do the code to count the players, but I don't know how to respond to a request from a website. Any help ?
    Thanks =)
     
  2. Offline

    DerpinLlama

    http://forums.bukkit.org/threads/admin-dev-tcp-interface-for-bukkit.746/
    You can use the playercount command without sending the password.

    PHP:
    <?php
    $bukkit_host 
    "localhost";
    $bukkit_port 6790;
    $bukkit_password "password";

    $errn "";
    $errs "";
    $timeout 10;
    $sock fsockopen($bukkit_host$bukkit_port$errn$errs$timeout);
    $expectplayercount false;
    if(
    $sock){
        while(!
    feof($sock)){
            
    fgetc($sock);
            
    fgetc($sock); //remove the first two chars
            
    $line trim(mb_convert_encoding(fgets($sock4096), "ASCII"));
            if(
    $line != ""){
                if(
    $expectplayercount == true){
                    if(
    $line != "1"){
                        echo 
    $line." players online.\r\n";
                    }
                    else{
                        echo 
    "1 player online.\r\n";
                    }
                    
    fclose($sock);
                    exit;
                }
                if(
    $line == "Welcome to Bukkit, please authenticate."){
                    
    fwrite($sock"playercount\r\n");
                    
    $expectplayercount true;
                }
            }
        }
    }
    else{
        echo 
    "Error: Could not connect to Bukkit! ".$errn." (".$errs.")";
    }
    ?>
     
  3. Offline

    ryanshawty

    I want to create a plugin that will return an array of players when I use http://<serverip>:<port>/players or submit the players to a website (every 5 mins) so I can use $_GET
     
  4. Offline

    DerpinLlama

    PHP:
    <?php
    $bukkit_host 
    "localhost";
    $bukkit_port 6790;
    $bukkit_password "password";

    $errn "";
    $errs "";
    $timeout 10;
    $sock fsockopen($bukkit_host$bukkit_port$errn$errs$timeout);
    $expectplayers false;
    $players = array();
    if(
    $sock){
        while(!
    feof($sock)){
            
    fgetc($sock);
            
    fgetc($sock); //remove the first two chars
            
    $line trim(mb_convert_encoding(fgets($sock4096), "ASCII"));
            if(
    $line != ""){
                if(
    $line == "Welcome to Bukkit, please authenticate."){
                    
    fwrite($sock"getplayers\r\n");
                    
    fgetc($sock);
                    
    fgetc($sock);
                    
    $line fgets($sock4096);
                    while(
    preg_match("/player name=/",$line)){
                        
    array_push($playerstrim(substr($line12)));
                        
    fgetc($sock);
                        
    fgetc($sock);
                        
    $line fgets($sock4096);
                    }
                    echo 
    json_encode($players);
                    
    fwrite($sock"quit\r\n");
                    
    fclose($sock);
                    exit;
                }
            }
        }
    }
    else{
        echo 
    "Error: Could not connect to Bukkit! ".$errn." (".$errs.")";
    }
    ?>
     
  5. Offline

    ryanshawty

    I will use that thanks, my minecraft server doesn't use Bukkit yet though :p
     
  6. Offline

    DerpinLlama

    Well uh, you can take code from my plugin and remove everything that's not needed. Password isn't needed for the getplayers, playercount, port, or maxplayers commands.

    Edit: K.
     
  7. Offline

    ryanshawty

    Thanks for help, I have implemented this into the new server status checker I am currently working on, Gotta redesign layout now ^^
     
Thread Status:
Not open for further replies.

Share This Page