XML Parsing and choosing an item in array

Hi!

So I’m looking to make a png of a game server’s status using xml provided by the game server hosting company. None of the code has really been written by me, I’m just cobbling it together for my website the best I can from what I can find. I’ll show all the info and hopefully someone can offer some advice:

The xml looks like this:

<?xml version="1.0" encoding="iso-8859-1"?> <qstat> <server type="TS3" address="xx.xxx.xxx.xx:xxxxx" status="UP"> <hostname>xx.xxx.xxx.xx:xxxxx</hostname> <name>Multiplay :: Mumble Comms</name> <gametype></gametype> <map>N/A</map> <numplayers>2</numplayers> <maxplayers>8</maxplayers> <numspectators>0</numspectators> <maxspectators>0</maxspectators> <ping>0</ping> <retries>0</retries> </server> <server type="A2S" address="yy.yyy.yyy.yy:yyyyy" status="UP"> <hostname>yy.yyy.yyy.yy:yyyyy</hostname> <name>Multiplay :: Bukkit powered Minecraft</name> <gametype>minecraft</gametype> <map>world</map> <numplayers>0</numplayers> <maxplayers>8</maxplayers> <numspectators>0</numspectators> <maxspectators>0</maxspectators> <ping>2</ping> <retries>0</retries> </server> </qstat>
There are two servers here, first is a mumble voice comms and second is the game (minecraft!).

The php I have reads this and gets the name, address, status and players online/max then writes it onto a png. However, it reads both servers and writes the information on top of each other so it’s a mess.

What I’d like is some function that allows me to specify which server’s details it outputs. This could be based upon position in array, so minecraft server would be 2nd, or based upon a value read from the XML. e.g. gametype=minecraft.

Here’s the PHP, let me know if there’s anything else I can add to help and I really appreciate anyone’s efforts. (I’ve hidden some IP/account info)

[php]

<?php Header("Content-Type: image/png"); $accountNumber = ZZZZZ; $xmlurl = "http://clanforge.multiplay.co.uk/public/servers.pl?event=Online;opt=ServersXmlList;accountserviceid=${accountNumber};fake=servers.xml"; $port = "XXXXX"; $img = imagecreatefrompng("http://.../image.png"); $gameServers = array(); $currentServer = null; $red = ImageColorAllocate($img, 192, 0, 0); $green = ImageColorAllocate($img, 0, 192, 0); $white = ImageColorAllocate($img, 255, 255, 255); $black= ImageColorAllocate($img, 0, 0, 0); $font_height = ImageFontHeight(3); $font_width = ImageFontWidth(3); $image_height = ImageSY($img); $image_width = ImageSX($img); $Length = $font_width*strlen($text); $image_center_x = ($image_width/2)-($length/2); $image_center_y = ($image_height/2)-($font_height/2); $XMLReader = new XMLReader(); $XMLReader->open($xmlurl); while ($XMLReader->read()) { $name = $XMLReader->name; $value = $XMLReader->readString(); switch ($name) { case "#text": case "qstat": break; case "server": if ($currentServer != $XMLReader->getAttribute('address')) $currentServer = $XMLReader->getAttribute('address'); if (!array_key_exists($currentServer, $gameServers)) { $gameServers [$currentServer]= array( 'name' => '', 'address' => $XMLReader->getAttribute('address'), 'gametype' => $XMLReader->getAttribute('gametype'), 'status' => $XMLReader->getAttribute('status'), 'ping' => 0, 'retries' => 0, 'map' => '_none', 'numplayers' => $XMLReader->getAttribute('numplayers'), 'maxplayers' => 0, 'numspectators' => 0, 'maxspectators' => 0, ); } break; default: if (empty($value)) break; $gameServers[$currentServer][$name] = $value; break; } } $XMLReader->close(); if (count($gameServers) > 0) { foreach ($gameServers as &$server) { // Determine status if ($server['status'] == "DOWN" || $server['status'] == "TIMEOUT") { $server['name'] = "Server is down"; $server['map'] = "N/A"; } $sname = $server['name']; ImageString($img, 2, 60, 3, $sname, $white); $saddress = $server['address']; $saddarray = explode(':', $saddress); $saddress = $saddarray[0]; $saddress .= ":"; $saddress .= $port; ImageString($img, 2, 60, 16, $saddress, $white); $sstatus = $server['status']; if ($sstatus == "UP") { $sstatus = "ONLINE"; ImageString($img, 2, 60, 29, $sstatus, $green); } elseif ($sstatus == "DOWN") { $sstatus = "OFFLINE"; ImageString($img, 2, 60, 29, $sstatus, $red); } else { ImageString($img, 2, 60, 29, $sstatus, $red); } $splayers = $server['numplayers']; $smaxplayers = $server['maxplayers']; $spsentence = "Users online: ".$splayers."/".$smaxplayers; ImageString($img, 2, 60, 42, $spsentence, $white); } } else ImageString($img, 2, 60, 3, "No Server found.", $red); ImagePNG($img); ImageDestroy($img); ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service