Jim Taylor

Intuitive PHP for the caffeinated.

Archive for the ‘Advanced’ Category

XML Dom for WoW Armory

If you searched for this topic you probably want to get information from the WoW armory and do something with it. Below is a scrape of the WoW armory for my guild the uDogs on Ysondre server. There are lots of things that can be done with the information, and some of my plans include a guild roster on our website that is updated when visited with the most current member information. The name, level, and other details would be stored in the database and then compared. You could see a person’s level progression from one date to another. The original reason for this piece of code is that I am making a raid leader management console to help scheduling raids and keeping track of attendance. Anyhow, modify as needed and play around with it.

// We'll use curl to load the target page here.

  $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,
    "http://us.wowarmory.com/guild-info.xml?r=Ysondre&n=uDogs&p=1");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
    Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
    $output = curl_exec($ch);
    curl_close($ch);

    $doc = new DOMDocument();
    $doc->loadXML($output);

// lets loop through all of the names so we can grab the
// info and put it into an array

	    $memberList = $doc->getElementsByTagName("character");
    foreach($memberList as $toon)
    {

    $data = array(
    'NAME' => $toon->getAttribute("name"),
    'RACE' => $toon->getAttribute("raceId"),
    'LEVEL' => $toon->getAttribute("level"),
    'CLASS' => $toon->getAttribute("classId"),
    'GENDER' => $toon->getAttribute("genderId")
    );

	dbconnect(); 

// Here we'll check and see if they player already exists.
// If they dont exist we add them, otherwise we update their record.	

        $sql_check = "SELECT * FROM wow_character WHERE `NAME` =
                           '".$data['NAME']."' LIMIT 1";

	$result = mysql_query($sql_check);

 echo $data['NAME']."“;
    if(mysql_num_rows($result) == 1)
    {

    $row = mysql_fetch_array($result);

    $sql_update = “UPDATE wow_character SET LEVEL = ” . $data[’LEVEL’] .
                        ” WHERE ID = ” . $row[’ID’];
    $data_update = mysql_query($sql_update);

    } else {

    $sql_insert = “INSERT INTO wow_character
( NAME,GUILD,LEVEL,CLASS,GENDER,RACE) VALUES (’”.$data[’NAME’].”‘,
‘uDogs’,'”.$data[’LEVEL’].”‘,’”.$data[’CLASS’].”‘,
‘”.$data[’GENDER’].”‘,’”.$data[’RACE’] .”‘)”;

	if ( mysql_query($sql_insert) ) {
        echo “User “. $data[’NAME’] .” was added to the database.”; 

    } else { echo “Could not add “. $data[’NAME’] .” to the database.
                        Error. Query: $sql_insert”;
}
    }

    }

Now we have grabbed all of their armory info and can start using the data across the site.

xmlDOM