I would like to extract the value of the attribute "value" using the id tag.
My code:
<?php
$url = 'http://turni.tt-contact.com/Default.aspx';
$contents = htmlentities(file_get_contents($url));
echo $contents."
"; //html
$dom = new DOMDocument;
$dom->validateOnParse = true;
$dom->loadHTML($contents);
$dom->preserveWhiteSpace = false;
$data = $dom->getElementById("__VIEWSTATE");
echo $data->nodeValue;
?>
I would like the attribute "value" -> "THIS":
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="THIS">
but the code returns only the html code.
What do I need to change?
Also by modifying it to:
$xpath = new DOMXpath($dom);
$data = $xpath->query('//input[@id="__VIEWSTATE"]');
$node = $data->item(0);
echo $node->getAttribute('value');
I get this error:
Fatal error: Call to a member function getAttribute() on null
Try this :
$data->getAttribute('value');
$attrs = array();
for ($i = 0; $i < $data->attributes->length; ++$i){
$node = $data->attributes->item($i);
$attrs[$node->nodeName] = $node->nodeValue;
}
var_dump($attrs);
Don't use htmlentities
as it will change the document's HTML tags from : <html>
to <html>
and your document won't be HTML anymore, just a plain text full of <
and >
, and so the methods to get nodes won't work.