billblog
billblog

Reputation: 3

Cannot pull attribute using simpleXML with PHP

I am pulling values from an API using simple XML. Earlier calls worked fine but for the life of me I cannot get this last piece to pull data. I am making the call with:

$postq='https://api.eveonline.com/char/SkillQueue.xml.aspx?keyid='.$key.'&vCode='.$code;
$qpull = simplexml_load_file($postq);

An example of the expected output is:

<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2009-03-18 13:19:43</currentTime>
  <result>
    <rowset name="skillqueue" key="queuePosition" columns="queuePosition,typeID,level,startSP,endSP,startTime,endTime">
      <row queuePosition="1" typeID="11441" level="3" startSP="7072" endSP="40000" startTime="2009-03-18 02:01:06" endTime="2009-03-18 15:19:21" />
      <row queuePosition="2" typeID="20533" level="4" startSP="112000" endSP="633542" startTime="2009-03-18 15:19:21" endTime="2009-03-30 03:16:14" />
    </rowset>
  </result>
  <cachedUntil>2009-03-18 13:34:43</cachedUntil>
</eveapi>

The call works and I am currently at the following:

  foreach( $qpull -> result as $c1)
    {
        foreach( $c1 -> rowset as $c2)
        {

            foreach( $c2 -> row as $c3)
            {

                var_dump($c3);
            }
        }
    }

This may be over complicating how it needs to be done but this is after hours of trying different ways to do it.

This is an example of the output:

    object(SimpleXMLElement)[20]
  public '@attributes' => 
    array (size=7)
      'queuePosition' => string '0' (length=1)
      'typeID' => string '3425' (length=4)
      'level' => string '1' (length=1)
      'startSP' => string '0' (length=1)
      'endSP' => string '500' (length=3)
      'startTime' => string '2014-08-04 16:16:11' (length=19)
      'endTime' => string '2014-08-04 16:30:16' (length=19)
object(SimpleXMLElement)[21]
  public '@attributes' => 
    array (size=7)
      'queuePosition' => string '1' (length=1)
      'typeID' => string '11566' (length=5)
      'level' => string '1' (length=1)
      'startSP' => string '0' (length=1)
      'endSP' => string '500' (length=3)
      'startTime' => string '2014-08-04 16:30:16' (length=19)
      'endTime' => string '2014-08-04 16:44:21' (length=19)

However if I try to select a value using:

var_dump($c3->level);

I am just getting blanks and cannot work out what I am doing wrong.

Thanks.

Upvotes: 0

Views: 100

Answers (3)

Eduardo Romero
Eduardo Romero

Reputation: 1159

You should be able to do:

echo $c3['level'];

From the looks of the API, you could just do:

foreach($qpull->result->rowset->row as $r) { 
  echo $r['level'] ."\n";
}

Upvotes: 1

hlscalon
hlscalon

Reputation: 7552

You are trying to get an attr but this is not how it is done.

$c3->attributes()->level

This should show the right result for you.

Simplexml Attributes

Upvotes: 0

schemar
schemar

Reputation: 622

For attributes, use the attributes method: SimpleXMLElement::attributes

Specifically the following:

<?php
include 'example.php';

$movies = new SimpleXMLElement($xmlstr);

/* Access the <rating> nodes of the first movie.
 * Output the rating scale, too. */
foreach ($movies->movie[0]->rating as $rating) {
    switch((string) $rating['type']) { // Get attributes as element indices
    case 'thumbs':
        echo $rating, ' thumbs up';
        break;
    case 'stars':
        echo $rating, ' stars';
        break;
    }
}

For the example:

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#211;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
XML;

Upvotes: 1

Related Questions