PHP cURL多表单提交脚本


PHP cURL multiple form submit script

我正在尝试登录并使用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;
}
?>

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

有人知道我哪里错了吗?

尽量不要关闭第一个帖子和第二个帖子之间的会话。

只需修改url和张贴选项,您就可以更改

...
//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);