简体   繁体   中英

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);

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);

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.

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

PPS Don not use it for spam.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM