iordanis
iordanis

Reputation: 1284

Posting data with PHP cURL

If there are 2 form fields in a page with 2 different submit buttons how do I tell to cURL which form to submit?

Example

<form method="post" action="index.php">
   <input type="text" name="age">
   <input type="submit" name="agree">
   <input type="submit" name="disagree">
</form>

My code so far

//create array of data to be posted
$post_data['age'] = '5';
 
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

$post_string = implode ('&', $post_items);

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

Upvotes: 0

Views: 211

Answers (2)

sectus
sectus

Reputation: 15464

There is no needing to know which form in HTML must be posted.

Form can be determined by Method, Action and/or Set of fields.

P.S. Clicked named submits are sending too. So add post_data['agree'] = '';.

P.P.S. Don not use it for spam.

Upvotes: 1

Tim Groeneveld
Tim Groeneveld

Reputation: 9039

You can pass an array to CURLOPT_POSTFIELDS and let php's curl extension do the dirty work of encoding etc.

This saves you from encoding the string!

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_data);

Upvotes: 1

Related Questions