【问题标题】:Send and receive data form CURL发送和接收数据表单 CURL
【发布时间】:2012-08-31 06:01:03
【问题描述】:

我有简单的脚本:

<?php if($_POST){
    if($_POST['captcha'] == 'random'){ ?>
<div id="answer">
    <?php echo $_POST['captcha'] . uniqid(); ?>
</div>
<?php
    }

} ?>

<form name="contactform" method="post" action="">
    email: <input  type="text" name="email"> <br />
    ###IMAGE###: random <br />
    captcha: <input  type="text" name="captcha">
    <input type="submit" value="Submit">
</form>

验证码是随机的,但对于这个测试,我总是使用“随机”。这个脚本工作正常。

所以 - 我想打开这个网站并填写验证码并提交表单并从 DIV #answer 接收数据。

我创造:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost/script.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);

print_r($output);

这显示我的表单,但如果我填写验证码并提交表单,那么这不会处理 $_POST。

我该怎么做?我必须先填写验证码。

【问题讨论】:

    标签: php forms curl


    【解决方案1】:

    你忘记发送数据了

    $postdata = "email=".$username."&password=".$password."&captcha=random";
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/script.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
    curl_setopt ($ch, CURLOPT_POST, 1); 
    
    $output = curl_exec($ch);
    curl_close($ch);
    
    print_r($output);
    

    【讨论】:

      【解决方案2】:

      试试这个:-

      $email = $_POST["email"];
      $pass = $_POST["pass"];
      $captcha = $_POST["captcha"];
      $curlPost = array('email' => $email, 'password'=>'$pass', 'captcha'=>$captcha);
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, 'http://localhost/script.php');
      curl_setopt($ch, CURLOPT_HEADER, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
      $data = curl_exec();
      curl_close($ch);
      
      echo "<pre>";
      print_r($data);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多