中文博客 (10) 美国陆军 (8) 部队生活 (7) English Blog (4) PHP (4) World of Warcraft (4) XML (2) IT (1) Network (1) Nginx (1) OIF (1) Rogue (1) Win 7 (1) 伊拉克 (1) 假期 (1) 笑话 (1)

Sunday, November 1, 2009

How to receive guild member data from wowarmory

There are a lot of guild creation websites that can help guild website owners to pull information from wowarmory.com. But most of them have the limitation what you can put on their web server.

Some people like to run their guild website own their own server, and having trouble to extract data from wowarmory. It is really easy actually. All we need is a free web hosting site that support PHP, and that's it.




Wowarmory is written in XML with XSL transformation. XML (Extensible Markup Language) is a flexible way to create common information formats and share both the format and the data on the World Wide Web, intranets, and elsewhere. It stores all the data from their database so that we don't need to hack into blizzard's server to find what we want.

So how do we parse those data? This is where PHP comes into play. we will use a PHP function called cURL to do the trick. (Information about cURL can be found in http://php.net/manual/en/book.curl.php)

create a file called "getdata.php" and put following code in.


query = $query;
$this->server = $server;
$this->guild = $guild;
$this->guildie = $guildie;
$this->page = $page;
} // end of __construct()
public function pull_xml() {
// change the first part of the $url to the armory link that you need
if ( $this->query === 'roster' ) {
$url = 'http://www.wowarmory.com/guild-info.xml?r=' . $this->server . '&n=' . $this->guild . '&p=' . $this->page;
} elseif ( $this->query === 'character' ) {
$url = 'http://www.wowarmory.com/character-sheet.xml?r=' . $this->server . '&n=' . $this->guildie;
}
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt ($ch, CURLOPT_USERAGENT, self::BROWSER);

$url_string = curl_exec($ch);
curl_close($ch);
return simplexml_load_string($url_string);
} // end of pull_xml()
} // end class
?>


What it does is pulling all data from wowarmory whether is a list of members or a single character, and saved into a array. Class armory is the core thing there, in order to retrieve what we want, we will call this class later in order to get our data.

Now we will create a page that pull all guild members from wowarmory. create another file named "guildmember.php" in the same directory as "getdata.php":

'Name', raceId => 'Race', classId => 'Class', genderId => 'Gender', level => 'Level', rank => 'Rank', achPoints => 'AchPts',);
//$armory = new armory( [character or roster] , realm , [guild name or NULL] , character name , [page number (guilds only) or NULL];
$armory = new armory(roster, "Twisting+Nether", "B+A+M+F", NULL, NULL);
$xml = $armory->pull_xml();
foreach ($xml->guildInfo->guild->members->character as $char) {
while ( strlen($char['achPoints']) > 5 ) $char['achPoints'] = "0". $char['achPoints'];
$charArray[] = array (name => $char['name'], raceId => $char['raceId'], classId => $char['classId'], genderId => $char['genderId'], level => $char['level'], rank => $char['rank'], achPoints => $char['achPoints']);
}
echo '<table border = "1" cellpadding = "4" width = "700">
<tr><thead>
';
foreach ( $listArray as $key => $value ) {
if ( $key == $data && $method == "asc" ) {
$order['key'] = "desc";
$order['noum'] = "descend";
} else {
$order['key'] = "asc";
$order['noum'] = "ascend";
}
echo '<td>'. $value .'</a></td>';
}
echo '</thead></tr>';
for ( $i = 0; $i < count($charArray); $i++ ) {
echo '<tr>
<td style = "height: 20px;">'. $charArray[$i]['name'] .'</td><td>'. $charArray[$i]['raceId'] .'</td>
<td>'. $charArray[$i]['classId'] .'</td><td>'. $charArray[$i]['genderId'] .'</td>
<td>'. $charArray[$i]['level'] .'</td><td>'. $charArray[$i]['rank'] .'</td><td>'. intval($charArray[$i]['achPoints']) .'</td>
</tr>';
}
echo "</table>";
?>

Core code in this little script is

//$armory = new armory( [character or roster] , realm , [guild name or NULL] , character name , [page number (guilds only) or NULL];
$armory = new armory(roster, "Twisting+Nether", "B+A+M+F", NULL, NULL);

As we can see with each corresponding spot, Twisting+Nether is the realm name, and B+A+M+F is the guild name, we can put NULL instead to read every single character in the realm, which will be a lot. NOTE: "+" means there is a space between two words. we have to have it in order for cURL function to work correctly.

Name, Race, Class, Gender, Level, Rank, and Achievement points are the fields we want to display in our website.

Can we display more? Yes. As mentioned above, wowarmory stores all the data into the xml file. That's why it is so slow to load right? :) Then how do we know what else can be displayed? We can do a little test.

By switching

$xml = $armory->pull_xml();

with

$xml = $armory->pull_xml(); print_r($xml); exit;

We will pull all data and print into the web page. It will look like the image below.


Really ugly and hard to see right? Here is a little trick we can do for a better view. Every browser has a view source code feature, look at page source, we can see a better formatted page display like this.

Much easier to read.

An important note: all race, class, gender, and rank are numeric value, we have to create our own function to replace them into image or word display.
With a little bit css style sheet's help, we can create a nice looking page displaying guild members. (Demo is at http://bamf.x10hosting.com/?function=guild&id=armory)





--------------------------
个人网页:http://akuma.x10hosting.com
魔兽公会网站:http://bamf.x10hosting.com

No comments:

Post a Comment