title: Get xml attribute using simplexml_load_string
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml">Accessing @attribute from SimpleXML</a>  </p>
  </blockquote>
  
  
  
  <p>I am using a few third party APIs which returns errors via xml in the following form:</p>
  
  <pre><code>&lt;xml&gt;
  &lt;status&gt;0&lt;/status&gt;
  &lt;error code="111"&gt;Error message text goes here.&lt;/error&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p>Using <code>simplexml_load_string</code> in PHP I can easily get the status 0 and the error message text but I cannot find a means of retrieving the <code>code="111"</code> value from the <code>&lt;error code="111"&gt;</code>. It seems to get dropped by SimpleXML.</p>
  
  <pre><code>&lt;?php
      $bytesRead = file_get_contents('http://api.....');
      $xml = simplexml_load_string($bytesRead);
  
      echo '&lt;pre&gt;'; print_r($xml); echo '&lt;/pre&gt;';
  ?&gt;
  </code></pre>
  
  <p>Outputs</p>
  
  <pre><code>SimpleXMLElement Object
  (
      [status] =&gt; 0
      [error] =&gt; Error message text goes here.
  )
  </code></pre>
  
  <p>Am I missing something? Is there a way to obtain this value or can someone suggest another method to get this?</p>