Jump to content

XML w/ cURL & PHP Help


Shadow Wolf

Recommended Posts

Using XML grabber with cURL I am able to load information from the XML page and convert it into a variable that I can work with in php. This is great for creating roster page or list based on armory.worldofwarcraft.com without having to manually enter in each members name or remake it every time someone joins. It will automatically update itself.

 

Base Code:

<php?
class armory {

const BROWSER="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.2) Gecko/20070319 Firefox/2.0.0.3";

public $query;
public $server;
public $guild;
public $guildie;
public $page;

public function __construct ( $query, $server, $guild, $guildie, $page ) {
    $this->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://eu.wowarmory.com/guild-info.xml?r=' . urlencode($this->server) . '&n=' . urlencode($this->guild) . '&p=' . $this->page;
        
      }elseif( $this->query === 'character' ){
        $url = 'http://eu.wowarmory.com/character-sheet.xml?r=' . urlencode($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
?>  

 

I can then sort the xml data and have it display information in a table format (still work in progress) just be adding this to the code:

$armory = new armory('roster', 'Drenden', 'Priests of Discord', NULL, NULL);
$xml = $armory->pull_xml();

  echo "<table>";
foreach ($xml->guildInfo->guild->members->character as $char) {
  echo "<tr id=\"".$char['name']."\">";
  echo "<td class=\"rightAlign\">".$char['name']."</td>";
  echo "<td>".$char['level']."</td>";
  echo "<td>".$char['class']."</td>";
  echo "<td>".$char['gender']."</td>";
  echo "<td>".$char['race']."</td>";
  echo "<td>".$char['achPoints']."</td>";
  echo "<td>".$char['url']."</td><br />";
  echo "</tr>";
		}
  echo "</table>";

 

I understand that the following:

$armory = new armory('roster', 'Drenden', 'Priests of Discord', NULL, NULL);
$xml = $armory->pull_xml();

is broken up into variables that let me define where it's pulling the data. There are two different XML pages, one is 'roster' and the other is 'character'. The second field determines what server: 'Drenden'. The third field indicates the guild name: 'Priests of Discord'. The fourth field indicates specific character name: NULL. The last field is page number, when looking at guild information only used during the 'roster' option.

 

The problem is the 'character' xml page. There is a lot more information in that page. I could access it for a single character like 'Anastasia' by using the following code:

$armory = new armory('character', 'Drenden', 'Priests of Discord', 'Anastasia', NULL);
$xml = $armory->pull_xml();

 

I want to be able to temporarily load all the XML data for the 'roster' page as well as each 'character' page for only those members on that 'roster' page. I tried to do something like:

 

Load Armory Roster Page

$armory = new armory('roster', 'Drenden', 'Priests of Discord', NULL, NULL);
$xml = $armory->pull_xml();

 

Then use access the name variable to create a separate query for each by doing:

foreach ($xml->guildInfo->guild->members->character as $char) {
$armory = new armory('character', 'Drenden', 'Priests of Discord', $char.['name'], NULL);
$xml = $armory->pull_xml();
	}

 

Unfortunately that does not seem to work. I'm probably trying to load too much variable information or I have the wrong order of how I can use it. I might be over complicating it too. Step by step I can produce each thing individually, now I'm just trying to combine all the single steps so I can display it all on one page.

 

 

 

Full Code:

<?php

class armory {

const BROWSER="Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.2) Gecko/20070319 Firefox/2.0.0.3";

public $query;
public $server;
public $guild;
public $guildie;
public $page;

public function __construct ( $query, $server, $guild, $guildie, $page ) {
    $this->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=' . urlencode($this->server) . '&n=' . urlencode($this->guild) . '&p=' . $this->page;
        
      }elseif( $this->query === 'character' ){
        $url = 'http://www.wowarmory.com/character-sheet.xml?r=' . urlencode($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  

$armory = new armory('roster', 'Drenden', 'Priests of Discord', NULL, NULL);
$xml = $armory->pull_xml();

    foreach ($xml->guildInfo->guild->members->character as $char) {
$armory = new armory('character', 'Drenden', 'Priests of Discord', $char['name'], NULL);
$xml = $armory->pull_xml();
	}

foreach ($xml->characterInfo->characterTab->professions->skill as $prof) {
echo $prof['name'];
}

foreach ($xml->guildInfo->guild->members->character as $char) {
  echo "<tr id=\"".$char['name']."\">";
  echo "<td class=\"rightAlign\">".$char['name']."</td>";
  echo "<td>".$char['level']."</td>";
  echo "<td>".$char['class']."</td>";
  echo "<td>".$char['gender']."</td>";
  echo "<td>".$char['race']."</td>";
  echo "<td>".$char['achPoints']."</td>";
  echo "<td>".$char['url']."</td><br />";
  echo "</tr>";
		}

?>

Link to comment
https://forums.phpfreaks.com/topic/140055-xml-w-curl-php-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.