简体   繁体   English

使用PHP cURL发布数据

[英]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? 如果页面中有2个表单字段,其中包含2个不同的提交按钮,如何告知cURL要提交哪个表单?

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. 您可以将数组传递给CURLOPT_POSTFIELDS并让php的curl扩展执行编码等的脏工作。

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. 无需知道HTML中的哪个表单必须发布。

Form can be determined by Method, Action and/or Set of fields. 表格可以通过方法,动作和/或字段集来确定。

PS Clicked named submits are sending too. PS Clicked命名的提交也在发送。 So add post_data['agree'] = ''; 所以添加post_data['agree'] = ''; .

PPS Don not use it for spam. PPS不要将它用于垃圾邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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