简体   繁体   English

cURL POST未提交表单

[英]cURL POST not submitting the form

I'm doing a project and one of the requests is to automatically submit a form to a random site, which is specified from time to time 我正在做一个项目,要求之一是自动将表单提交到不定期指定的随机站点

This is my cURL code : 这是我的cURL代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dati_post);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

Code to get POST parameters : 获取POST参数的代码:

  foreach ($html->find('form[method=post],form[method=POST]') as $forms_post) {
  $n_formPOST++;
  $formPOST_action = $forms_post->action;
   foreach ($forms_post->find('input') as $input) {
     if ($input->type == 'text') {
       $dati_testo[$input->name] = "text";
     } else if ($input->type == 'email' || $input->name == 'email') {
       $dati_random[$input->name] = "emailrandom@gmail.com";
     } else if ($input->type == 'hidden') {
       $dati_random[$input->name] = $input->value;
     } else {
       $dati_random[$input->name] = "random";
     }
   }
   foreach ($forms_post->find('textarea') as $textarea) {
     if ($textarea->disabled != true) {
       $dati_testo[$textarea->name] = "text";
     }
   }
   foreach ($forms_post->find('button') as $bottone) {
     if ($bottone->type == 'submit') {
       $dati_random[$bottone->name] = "random";
     }
   }

The problem is that in some sites POST is done correctly and I receive the right answer, which corresponds to the answer I would receive by doing it manually. 问题是在某些站点中POST正确完成,并且我收到正确的答案,该答案与手动完成操作会收到的答案相对应。 On other sites, it seems that the form is not submitted. 在其他站点上,似乎未提交该表单。 I have repeatedly checked the URL that I insert in the cURL and also the data that I pass and if I use them manually it works. 我已经反复检查了我在cURL中插入的URL,以及通过的数据,如果我手动使用它们,它将起作用。 I even tried using online tools that perform POST / GET passing the same URL and the same data that I get in my project and it works. 我什至尝试使用执行POST / GET的在线工具,并传递与我在项目中获得的相同的URL和相同的数据,并且该工具可以正常工作。

The url_post is made from url host+form action. url_post由url host + form动作组成。 I don't understand if there is something wrong in my curl code, considering I'm pretty sure the data I'm passing to the curl are corrects to complete the POST. 我不知道我的curl代码中是否有问题,因为我很确定我传递给curl的数据正确无误,可以完成POST。

Data : 资料:

You need to use the curl_exec() function in order to execute the cURL. 您需要使用curl_exec()函数才能执行cURL。 It takes the $ch param, like such: 它需要$ch参数,如下所示:

// Execute cURL
curl_exec($ch);

// Close cURL
curl_close($ch);

More info here 更多信息在这里

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

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