Monday 24 September 2018

PHP : Get name and value of all input tags on a page with DomDocument

The following code snippet will extract all input tag names and values as an associative array, from a given html page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
    Generic function to fetch all input tags (name and value) on a page
Useful when writing automatic login bots/scrapers
*/
function get_input_tags($html)
{
    $post_data = array();
     
    // a new dom object
    $dom = new DomDocument;
     
    //load the html into the object
    $dom->loadHTML($html);
    //discard white space
    $dom->preserveWhiteSpace = false;
     
    //all input tags as a list
    $input_tags = $dom->getElementsByTagName('input');
 
    //get all rows from the table
    for ($i = 0; $i < $input_tags->length; $i++)
    {
        if( is_object($input_tags->item($i)) )
        {
            $name = $value = '';
            $name_o = $input_tags->item($i)->attributes->getNamedItem('name');
            if(is_object($name_o))
            {
                $name = $name_o->value;
                 
                $value_o = $input_tags->item($i)->attributes->getNamedItem('value');
                if(is_object($value_o))
                {
                    $value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
                }
                 
                $post_data[$name] = $value;
            }
        }
    }
     
    return $post_data;
}
 
/*
    Usage
*/
 
error_reporting(~E_WARNING);
$html = file_get_contents("https://accounts.google.com/ServiceLoginAuth");
 
echo "<pre>";
print_r(get_input_tags($html));
echo "</pre>";

0 comments:

Post a Comment