title: PHP DOMDocument: Get attribute value from id
body: <p>I would like to extract the value of the attribute "value" using the id tag.</p>
  
  <p>My code:</p>
  
  <pre><code>&lt;?php
   $url = 'http://turni.tt-contact.com/Default.aspx';
   $contents = htmlentities(file_get_contents($url));
   echo $contents."\n"; //html
   $dom = new DOMDocument;
   $dom-&gt;validateOnParse = true;
   $dom-&gt;loadHTML($contents);
   $dom-&gt;preserveWhiteSpace = false;
   $data = $dom-&gt;getElementById("__VIEWSTATE");
   echo $data-&gt;nodeValue;
  ?&gt;
  </code></pre>
  
  <p>I would like the attribute "value" -> "THIS": </p>
  
  <pre><code>   &lt;input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="THIS"&gt;
  </code></pre>
  
  <p>but the code returns only the html code.</p>
  
  <p>What do I need to change?</p>
  
  <p>Also by modifying it to:</p>
  
  <pre><code>$xpath = new DOMXpath($dom);
  $data = $xpath-&gt;query('//input[@id="__VIEWSTATE"]');
  $node = $data-&gt;item(0);
  echo $node-&gt;getAttribute('value');
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>  Fatal error: Call to a member function getAttribute() on null 
  </code></pre>