使用curl php在表单后发送表单

时间:2016-11-02 20:18:54

标签: php forms curl

我有一个问题要问你。 我有这段代码:

        $postData2 = array(  
        'From'=>'20/10/2016',
        'To' =>'30/10/2016'
      );  

      $postData = array(  
        'User' =>'Sebastian',
        'Password' =>'12345'
      );  

//1.- login...
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://web.com/login.php');
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-CL; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
    curl_exec ($ch);

    //2.- send dates...
    curl_setopt($ch, CURLOPT_URL, 'https://web.com/askdates.php');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec ($ch);

    //3.- answer...
    curl_setopt($ch, CURLOPT_URL, 'https://web.com/answer.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; es-CL; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
    $page=curl_exec($ch);

    curl_close($ch);
    echo $page;

第一步是好的..登录是正确的。 我认为第二步是好的,但第二种形式的答案显示在https://web.com/answer.php中,所以我写了3步......但我只看到https://web.com/answer.php没有发布信息。

我需要做些什么来纠正这个问题?

PS:首先,我写了两个第一步,但答案是否定的。 谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解您的问题,但如果您尝试同时发送多个卷曲请求,请执行以下操作:

<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

?>

所以基本上创建每个单独的请求但不执行它们然后使用curl_multi_init()将请求一起加入到一个请求之后执行它。

如果您想了解如何阅读此处的详细信息:http://php.net/curl_multi_init