【问题标题】:Auto-submit form using cron job使用 cron 作业自动提交表单
【发布时间】:2017-11-06 23:59:13
【问题描述】:

当我使用网络浏览器执行它时,我的代码运行良好,但是当我尝试使用 wget、curl 或 python 脚本执行它时,它无法正常运行。 php代码已执行,但表单未自动提交。

<?php
$dbh = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');

$reponse = $dbh->query('SELECT code FROM download limit 1');
$donnees = $reponse->fetch();
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
   <title>validation de formulaire</title>
   <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
   <style type="text/css">
   </style>
</head>
<body onload="document.f.submit()">
  <form name="f" method="post" action="http://website.lol/upload/traitement.php">
    <input type="text" class="form-control" id="url" name="url" placeholder="http://..." value="https://url.com/<?php echo $donnees['code']; ?>">
  </form>
</body>
</html>

也在javascript中尝试过:

<script type="text/javascript">
        document.forms["f"].submit();
      </script>

但结果相同。

【问题讨论】:

  • curl、wget等不执行javascript。您需要从脚本发出curl请求才能提交表单。

标签: javascript php python cron


【解决方案1】:

使用 curl 发布数据

<?php

$url = 'http://website.lol/upload/traitement.php';

$fields = array(
   'url' => urlencode("your_values_here"),
);

//url-ify the data for the POST
 foreach($fields as $key=>$value) { 
    $fields_string .= $key.'='.$value.'&'; 
 }
 rtrim($fields_string, '&');

 //open connection
 $ch = curl_init();

 //set the url, number of POST vars, POST data
 curl_setopt($ch,CURLOPT_URL, $url);
 curl_setopt($ch,CURLOPT_POST, count($fields));
 curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

 //execute post
 $result = curl_exec($ch);

 //close connection
 curl_close($ch);

?>

【讨论】:

    猜你喜欢
    • 2016-01-05
    • 2012-03-13
    • 2023-04-04
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    相关资源
    最近更新 更多