2014-09-24 106 views
0

我正在尝试使用PHP 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; 

} 
?> 

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

有没有人知道我要去哪里错了?

+0

首先从第一卷曲结果卷曲响应然后调用第二卷曲。 – 2014-09-24 12:10:40

+0

是的,我明白了。此外,我已经分配了相同的变量** $结果**。我在处理第二种形式时遇到了问题。 – 2014-09-24 12:14:56

回答

0

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

而且只需要修改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);