Dumb_Shock
Dumb_Shock

Reputation: 1008

Filling form field

I want to fill a form field and submit it using CURL.The webpage has many form fields which are already filled so i do not want to touch these fields.

So is it possible to use curl and fill only the required field and submit it along with all other fields which are already filled ?

Upvotes: 2

Views: 1045

Answers (3)

user895378
user895378

Reputation:

A full walkthrough (given the supplied HTML scrape text)

To customize you would simply add field names to the $fields_i_want array to specify all of the text field values you wanted to pull from the downloaded source text and change the URLs for retrieval and submission locations.

Also, a better alternative to file_get_contents() is curl. You can use the instructions on this SO post for how to retrieve remote text via curl.

// First, retrieve the remote source using file_get_contents()
$str = file_get_contents('http://www.example.com/');    

$my_field_val  = 'my_field_value';
$fields_i_want = array('audien', 'unifor');
$field_vals    = array();
$field_string  = '';


// Use DOM to parse the values you want from the form
$dom = new DOMDocument;
$dom->loadHTML($str);

// Get all the input field nodes
$inputs = $dom->getElementsByTagName('input');

// Iterate over the input fields and save the values we want to an array
foreach ($inputs as $input) {
  $name = $input->getAttribute('name');
  if (in_array($name, $fields_i_want)) {
    $val = $input->getAttribute('value');
    $field_vals[$name] = $val;
  }
}

// append the field value we set ourselves to the list
$field_vals['my_field'] = $my_field_val;

foreach ($field_vals as $key => $val) {
  $field_vals[$key] = urlencode($val)
}

// url-ify the data for the POST
foreach($fields as $key=>$value) {
  $fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');

// open connection
$ch = curl_init();

// POST the data to the form submission url
$submit_url = 'http://www.submitform.com';

// set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

// execute post (returns TRUE on success or FALSE on failure)
$result = curl_exec($ch);

// close connection
curl_close($ch);

Upvotes: 2

Brad Christie
Brad Christie

Reputation: 101604

Depends how the form is initially populated.

If the pre-populated elements all use value="foo" then just grab the page (using curl), load it up in to a DOMDocument, fetch the <form> you're after and populate the field(s) you need, then pass it off as another request using a new cURL request (taking in to account the form's action and method attributes, as well as form data being sent off).

However, if they're populated with JS and you don't plan on writing the cURL request to mimic what you're doing on your browser, I don't see an easy way of mimicking JS actions, then populating, then sending it off.

Also, this doesn't take in to account any cookies that may be present. If you need those, you're going to have to store them from the first request and make sure to send them off in the actual submission call.

Upvotes: 2

camilo_u
camilo_u

Reputation: 1380

Not sure if it fits, but in order to check what are the required fields you need to parse the HTML and check for each input file and look for any required mention.

To parse the HTML, you can use some tools like:

http://framework.zend.com/manual/en/zend.dom.query.html

http://simplehtmldom.sourceforge.net/

With that tools, you can open the page, look for any required tags on the fields, and then decide with form field to submit.

Upvotes: 1

Related Questions