Chris
Chris

Reputation: 3025

Fetching data from a site requiring POST data?

There's a very simple website containing just a few elements of which I'd like to retrieve a table. Previously I've used file_get_contents('http://www.example.com') for this exact purpose and extracted the piece of information I needed through explode and str_replace commands, which worked fine.

However, this website requires POST data before it will display the table I require. I know only the name and id of the select boxes through which the data is submitted, as well as the 'option value' I need to submit.

Another thing is that the second select box only appears once the first one has been dealt with, much like the table only appears once an option has been chosen for both.

How do I go about fetching the table?

Thanks in advance!

Abstract (and poor) representation of the site serving the table:

    <select id='select_box_1' name='first select box'>
      <option value='option1_1'>Thing one</option>
      <option value='option1_2'>Second thing</option>
      <option value='option1_3'>Thing number three</option>
    </select>

    NOTE: Selecting an option will spawn:

    <select id='select_box_2' name='second select box'>
      <option value='option2_1'>First thing</option>
      <option value='option2_2'>Second choice</option>
      <option value='option2_3'>The third option</option>
    </select>

    NOTE: Selecting an option will spawn the table I need.

Upvotes: 1

Views: 197

Answers (2)

ChrisR
ChrisR

Reputation: 14447

My guess is that the site you are trying to scrape is storing the value of the first select in a session after a POST to populate/show the second select. This means it can't be done with a single request.

You'll have to use a PHP Http client that support POST and Sessions/Cookies. Zend_Http_Client from ZendFramework supports both so is a good candidate.

Upvotes: 1

Tudor Constantin
Tudor Constantin

Reputation: 26861

Try with curl:

Here are some examples

Upvotes: 3

Related Questions