phpweb-scraping

Parsing a page, getting the contents of an input field?


I want to open a page using PHP, and get two values from two input fields.

The input fields are like so:

<input type="hidden" name="__ONE" id="__ONE" value="/randomletters" />  
<input type="hidden" name="__TWO" id="__TWO" value="/randomletters" />

How would I be able to go about doing this?

Scraping this page, and returning the value for input "__ONE" as $one, and so on...


Solution

  • Use an HTML parser. http://simplehtmldom.sourceforge.net/ is one example.

    $html = file_get_html('mydocument.html');
    $values = array();
    foreach($html->find('input') as $element)
    {
        $values[$element->id] = $element->value;
    }
    
    print_r( $values );
    

    ...

    Array
    (
        [__ONE] => /randomletters
        [__TWO] => /randomletters
    )
    

    You could also try QueryPath or PHPQuery or Zend Framework, etc, etc. They're all good.

    Also, I would like to congratulate you for being the first person ever on stackoverflow NOT to start off by asking how you can accomplish this with regular expressions. :)