Online Games (Updated!)

Ever wanted to display the join URL, Players in the game and the server Status on a PHP-enabled website? Now you can!

In addition to my previous post, this little function will grab all the information on hosted servers you need. To use it is pretty simple too, Grab the data using a variable: $servers = hub_get_contents('enigmaster2002.iconultima'); then just put it inside a for($i = 0; $i < count($servers); $i++) loop and display the data with $servers[$i]['url'].

(An example of the script in action can be found: here. Just change the ?hub= in the address bar to your game and see how it works.

function hub_get_contents($hub) {
    $hub_page = file_get_contents("http://www.byond.com/games/" . $hub . "&format=text");
    $servers = array();

    $i = strpos($hub_page,"world/1");
    $disabledline_server = explode('world/', substr($hub_page, $i+9, strlen($hub_page)));
    foreach($online_server as $game) {
        $server = array(
            'url'           => '',
            'status'        => '',
            'users'         => '',
        );
        
        $start = strpos($game, 'url');
        if($start) {
            $a = strpos($game, '"', $start)+1;
            $b = strpos($game, '"', $a);
            $server['url'] = substr($game, $a, $b - $a);
        }
        
        $start = strpos($game, 'status');
        if($start) {
            $a = strpos($game, '"', $start) +1;
            $b = strpos($game, '"', $a);
            $server['status'] = substr($game, $a, $b - $a);
        }
        
        $start = strpos($game, 'users');
        if($start) {
            $start = $start+13;
            $end = strpos($game, ')', $start);
            $result = str_replace('"', '', substr($game, $start, $end - ($start)));
            $server['users'] = $result;
        }
                    
        $servers[] = $server;
    }
    
    return $servers;
}


[Update]
One or two people sent me a page because they were having trouble getting the function to work. So I'll give you a complete working script to go with it.

$servers = hub_get_contents('enigmaster2002.iconultima');
for($i = 0; $i < count($servers); $i++) {
    echo("<b>URL:</b> " . $servers[$i]['url'] . "<br>");
    echo("<b>Status:</b> " . $servers[$i]['status'] . "<br>");
    echo("<b>Users:</b> " . $servers[$i]['users'] . "<br><br>");
}

function hub_get_contents($hub) {
    $hub_page = file_get_contents("http://www.byond.com/games/" . $hub . "&format=text");
    $servers = array();

    $i = strpos($hub_page,"world/1");
    $online_server = explode('world/', substr($hub_page, $i+9, strlen($hub_page)));
    foreach($online_server as $game) {
        $server = array(
            'url'           => '',
            'status'        => '',
            'users'         => '',
        );
        
        $start = strpos($game, 'url');
        if($start) {
            $a = strpos($game, '"', $start)+1;
            $b = strpos($game, '"', $a);
            $server['url'] = substr($game, $a, $b - $a);
        }
        
        $start = strpos($game, 'status');
        if($start) {
            $a = strpos($game, '"', $start) +1;
            $b = strpos($game, '"', $a);
            $server['status'] = substr($game, $a, $b - $a);
        }
        
        $start = strpos($game, 'users');
        if($start) {
            $start = $start+13;
            $end = strpos($game, ')', $start);
            $result = str_replace('"', '', substr($game, $start, $end - ($start)));
            $server['users'] = $result;
        }
                    
        $servers[] = $server;
    }
    
    return $servers;
}

Posted by Tiberath (Guildmaster) on Sunday, February 17, 2008 01:24AM - 2 comments / Members say: yea +0, nay -0
(Edited on Tuesday, March 18, 2008 09:08PM)

« hub_get_contents() · Use PHP (and Python) to relaunch DreamDaemon »

Login to post a comment.

#2 Tiberath:  

I really hate that DM tags auto-convert HTML anchor tags, for the reccord.

Monday, February 18, 2008 02:47AM

#1 Devourer Of Souls:  

Awesome script, thanks again for helping me getting it to work!

Monday, February 18, 2008 02:27AM

PHP

Joined: Jul 27, 07

Blog Calendar

February 2008
Su Mo Tu We Th Fr Sa
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29  
 
«Dec Mar»