【问题标题】:PHP cURL multiple form submit scriptPHP cURL 多表单提交脚本
【发布时间】:2014-09-24 12:05:01
【问题描述】:

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

}
?>

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

有人知道我哪里出错了吗?

【问题讨论】:

  • 首先从第一个 curl 结果中获取 curl 响应,然后调用第二个 curl。
  • 是的,我明白了。我也将其分配给变量 $result。我在处理第二种形式时遇到问题。

标签: php forms curl


【解决方案1】:

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

只需修改您需要更改的网址和发布选项

...

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

【讨论】:

    猜你喜欢
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2014-08-23
    • 2018-01-12
    • 2011-02-27
    • 1970-01-01
    相关资源
    最近更新 更多