使用CURL提交表单 [英] Submit form using CURL
问题描述
如何使用curl在提交按钮上提交没有name属性的表单?
How do you submit a form using curl that has no name attribute on submit button?
例如,目标网站有以下提交按钮:
For example, the target site has the following submit button:
<input id='some' value='value' type='submit' >
这是我有的:
$post_data['name'] = 'xyz';
$post_data['some'] = 'value';
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
$ch = curl_init($web3);
//set options
//curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($ch);
推荐答案
c $ c> $ post_data ,将提交表单。
You (usually) do not need to encode $post_data
, and the code below will submit the form.
。当 curl_exec
运行时,服务器接收相当于填充表单(前提是 post_data
正确)。
The button is not necessary. When curl_exec
runs, the server receives the equivalent of a filled form (provided that post_data
is correct).
如果操作未继续,则会丢失某些字段,或者会话中的某些内容。
If the action does not proceed, either some field is missing, or maybe something in the session is. Try opening with cURL the same page that displays the form, then submitting the form.
$post_data['name'] = 'xyz';
$post_data['some'] = 'value';
$ch = curl_init();
//set options
//curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($ch,CURLOPT_POST, true);
// Note -- this will encode using www-form-data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_URL, $web3);
$result = curl_exec($ch);
UPDATE
因此,让我们看看正常情况和cURL会发生什么:
So let us see what happens normally and what happens with cURL:
Browser cURL
1. Goes to www.xyz.com/form curl_exec's a GET on www.xyz.com/form
2. Server sends HTML Server sends HTML
3. User types in fields We populate $post_data
4. User clicks "SUBMIT" We run curl_exec and POST $post_data
5. Browser contacts server cURL contacts server
6. Browser sends fields cURL sends fields
7. Server acts upon request Server acts upon request
8. Profit Profit
上述代码仅实现阶段3-6。阶段2也可能发送会话cookie并设置阶段7所需的一些数据。因此,您需要使用另一个curl_exec(GET方法,可能是这个时候)实现阶段1,然后才能成功请执行以下阶段。
The code above only implements phases 3-6. It is possible that phase 2 also sends a session cookie and sets some data, which are required by phase 7. So, you need to also implement phase 1 with another curl_exec (GET method, probably, this time), before being able to successfully execute the following phases.
这篇关于使用CURL提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!