2012-08-30 66 views
1

如何使用提交按钮上没有名称属性的curl提交表单?使用CURL提交表格

例如,目标站点有如下提交按钮:

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

提交按钮本身不必具有任何名称属性 - 只有字段本身。 – Lix

+0

但是如何提交呢?我编辑了代码..你可以举一个例子 – zista

+0

你必须发送帖子请求到那个你得到页面的响应URL。 – pce

回答

2

根本不提供输入表单字段的名称 - 一个浏览器实际上会为你做什么。

+0

所以你说如果我这样做$ post_data [''] ='value';它会提交表格? – zista

+0

你不需要这样设置,实际上你根本不需要设置这个“值”。只要将其余的表单域填入请求并发送即可。 – jdevelop

+0

但是如何提交呢?我编辑了代码..你能举一个例子 – zista

3

您(通常)不需要编码$post_data,并且以下的代码将提交表格。

该按钮不是必需的。当curl_exec运行时,服务器将收到填充表单的对等项(前提条件是post_data正确)。

如果操作没有进行,某个字段丢失,或者可能是会话中的某个字段。尝试用cURL打开与显示表单相同的页面,然后提交表单。

$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

那么,让我们看看会发生,通常会发生什么用卷曲:

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执行以下阶段。

+0

他已经用'curl_init($ web3);' – Lusitanian

+0

Oopsie设置_a_ url。那么,我不明白为什么它不应该工作,除非$ web3错误或某个字段丢失。 “表单被填充”的事实似乎表明服务器识别数据,但它不会继续。有些字段仍然丢失,或者某些会话信息是。 – LSerni

+0

是的,我不太明白这个问题。 – Lusitanian