Server Status Script

From Homecoming Wiki
Revision as of 06:17, 18 May 2009 by imported>Aggelakis (historical) ({{PlayerGuide}})
Jump to navigation Jump to search
Player Guide Notice

This article is a Player Guide. The Homecoming Wiki takes no responsibility for the content within.

Questions and concerns should be posed to the authors of the article using the article's talk page.

I'm just posting it here in hopes that folks from the CoH/V community can see it and use it to its fullest extent. Feel free to add upon this and post your fixes/ideas or other script languages that have the same function.

PHP Server Status Class

For PHP 5

<?php
/*
Timestamp:  5/4/2009 11:48 AM
Author:     Anthony W. Steiner (of the Titan Network)
Site:       http://cohtitan.com
email:      [email protected]

License:    None, I just threw this together for the COH/V community to no
            ends of my own.

            Do what you want to this script, no credit needs to stay intact.

Summary:    Well, the good folks at "Paragon Studios" finally made the server
            status a utilitarian XML sheet. This is just something to make it
            easier on the PHP coders out there that are still seeking some
            help on the matter.


USAGE
-------------------------------------------------------------------------------
Dependencies:
            - PHP Compatible Server
            - PHP 5 Mandatory
            - SimpleXMLElement (only works with PHP 5+)

Example(s): NOTE: (When editing, do no use the <> characters, used as
            denotation for user-defined variables that you will need to change
            in you own code.)

            This script is quite easy to use, simply include the php file
            and call the class to the page in question.

######################## [ Code Example 1 ] ########################
include ("<path to this file>");
$<variable> = New ServerStatus();
######################## [ Code Example 1 ] #######################

            Keep in mind, you can put the class right inside the page you wish
            to use this one and not have to include it.

            Now that it's called we can go forward with just throwing it in
            with some code.

            The $<variable> is an OBJECT, there for '->' is where the magic
            happens.

######################## [ Code Example 2 ] ########################
foreach( $<variable>->servers as $serverName => $serverStatus)
{
  echo $serverName . " - " . $serverStatus . "<br />";
}
######################## [ /Code Example 2 ] #######################

            You can use this method in a million different ways... your
            imagination is the limit.

            The other two possible values do not need any form of additional
            coding to write out.

######################## [ Full Page Example ] ########################
include ("<path to this file>");
$<variable> = New ServerStatus();

foreach( $<variable>->servers as $serverName => $serverStatus)
{
  echo $serverName . " - " . $serverStatus . "<br />";
}

echo $<variable>->lastUpdate;
echo $<variable>->timeZone;
######################## [ /Full Page Example] #######################


Values:
            $variable->servers
                - Object
                - Can be put in a loop (See "Code Example 2")
                - Uses Key => Value syntax for looping

            $variable->lastUpdate
                - String
                - Uses strtotime() function to assume host server's local timezone
                - Does Not require any additional code to call

*/

class ServerStatus
{
  public function __construct()
  {
      return $this->get();
  }

  public function get()
  {
      $remoteFeed       =  file_get_contents("http://www.cityofheroes.com/servers/server_stat.xml");

      $xml              = new SimpleXMLElement($remoteFeed);

      $lastUpdate = (string)$xml['timestamp'];
      $timeZone   = (string)$xml['timezone'];
      // Using the strtotime() function will allow you to convert the UTC time into your site's local time,
      // which can be set in the php.ini if your host/server allows this change.
      $this->lastUpdate = date('h:ia M j, Y', strtotime($lastUpdate.$timeZone));

      foreach($xml->server as $server)
      {
        $serverObject->$server['name'] = (string)$server['status'] ;

      }
      $this->servers    = $serverObject;

      return $this;
  }
}

$ServerStatus = New ServerStatus;
?>

A few different examples
<br /><br />
Basic Table
<table>
  <tr>
    <th>Server</th>
    <th>Status</th>
  </tr>
<?php foreach($ServerStatus->servers as $serverName => $serverStatus): ?>
  <tr>
    <td><?= $serverName ?></td>
    <td><?= $serverStatus ?></td>
  </tr>
<?php endforeach; ?>
</table>
Last Update: <?= $ServerStatus->lastUpdate ?>

Example | Source Page

Yea I only have one example... but if you get crafty with the code you can trade out text for images, or even make it so only the server(s) you want will show up.

Hit me up at [email protected] or PM me on the Titan Forums if you have any questions or requests!