PHP cURL多表单提交脚本

时间:2014-09-24 12:05:02

标签: php forms curl

我尝试使用PHP cURL登录并提交另一个联系表单。

以下是我的脚本,

<?php

echo login_curl();

function login_curl() {

    //First Form post data's

    $postfields = array();
    $postfields["formUsername"] = urlencode("xxx");
    $postfields["formPassword"] = urlencode("xxx123");    


    //Define option variables
    $action ="http://www.websss.com/websss/authenticate.php";
    $cookie = "cookie.txt";
    $agent = "Mozilla/26.0";
    $referer = "http://www.websss.com/websss/login.php";


    //Second form post data's     
    $secpostfields = array();
    $secpostfields["form_url"] = urlencode("http://web.com");
    $secpostfields["form_desc"] = urlencode("Jquery web Link");

    //Define option variables
    $secondaction ="http://www.websss.com/websss/insert_link.php";            
    $secondreferer = "http://www.websss.com/websss/login.php";        


    // Submit Login Form
    $ch = curl_init($action);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_REFERER, $referer);    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    //Capture output and close session
    $result = curl_exec($ch);
    curl_close($ch);

    //submit second form after login
    $secondch = curl_init($secondaction);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $secpostfields);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_REFERER, $secondreferer);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    //Capture output and close session
    $resultsecond = curl_exec($secondch);
    curl_close($secondch);

    return $resultsecond;

}
?>

我可以注意到,上面的脚本正确登录,但它没有提交第二张表格。

有谁知道我哪里出错?

1 个答案:

答案 0 :(得分:0)

请尝试不要在第一篇和第二篇文章之间关闭会话。

只需修改您需要更改的网址和帖子选项

...

//Capture output and don't close session
$result = curl_exec($ch);
//curl_close($ch);

//submit second form after login

curl_setopt($ch, CURLOPT_URL, $secondaction);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $secpostfields);
curl_setopt($ch, CURLOPT_REFERER, $secondreferer);
$resultsecond = curl_exec($ch);