Page 1 of 1

PHP DOM

Posted: Wed Aug 22, 2007 2:34 am
by d.shankar
The below code i posted fetches an html page and searches for the occurence of forms .. And if found the form values are printed

But i need to print also the input text variables associated with the form [they should be mutually dependent]

below is the code

Code: Select all

<?php

$target_url = "www.dnschart.com";

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);

$dom = new DOMDocument();
@$dom->loadHTML($html);

$params = $dom->getElementsByTagName('form');

foreach ($params as $param) {
       echo $param -> getAttribute('name').'<br>';
       if($param->hasChildNodes())
       {
	       echo "true";
	       echo "<br>";
	       $children = $param->childNodes;
	       echo $children->getElementsByTagName('input').'<br>';
	       foreach($children as $child)
	       {
		       echo $child->getAttribute('name');
	       }
      }
else
{
echo "false";
echo "<br>";
}
?>
Waiting for reply.

Re: PHP DOM

Posted: Wed Aug 22, 2007 4:54 am
by volka
d.shankar wrote:But i need to print also the input text variables associated with the form [they should be mutually dependent]
Do you mean the value attribute of the input element?

Posted: Wed Aug 22, 2007 5:33 am
by Ollie Saunders
I would use SimpleXML and XPath to do this. Generally the DOM is really awkward for conditionally finding stuff.

I think one of these will be the xpath you are after:

Code: Select all

form//.[@name]
form//*[@name]
form//@name
This could be a function:

Code: Select all

function urlContents($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, 'User agent here');
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    return curl_exec($ch); 
}

Re: PHP DOM

Posted: Wed Aug 22, 2007 6:22 am
by d.shankar
volka wrote:
d.shankar wrote:But i need to print also the input text variables associated with the form [they should be mutually dependent]
Do you mean the value attribute of the input element?
No i need the variable names. I mean.

An example...

Code: Select all

<form action='1.php' method='post' name='frm1'>
<input type='text' name='txt1'>
</form>
<form action='2.php' method='post name='frm2'>
<input type='text' name='txt2'>
</form>

Now i have to retrieve

1.php
txt1

2.php
txt2