dousong8187 2012-06-28 11:32
浏览 76
已采纳

使用Curl登录网站,转到链接,再次提交表单并获取输出

I am trying to log into a website, then go to a link and then submit a form to get the desired data. I want to do it using cURL. I have achieved success in loggin into the website. The login redirects me to the profile page.

Now I need to follow a link and then submit a form! but when i do that using CURL the session is invalidated. I get the JSESSIONID in a cookie.txt file i used to store the cookie created. All the examples i have seen are just about loggin into a website or just a registration form submission.that is just one single POST request!

How do I go to another link and then submit another form using curl after I have successfully logged in and stored the cookie?

I am using WAMP as my local server.

<?php
$username="myusername"; 
$password="mypassword"; 
$url="http://onlinelic.in/LICEPS/Login/webLogin.do";
$referer = "http://onlinelic.in/epslogin.htm"; 
$postdata = "portlet_5_6{actionForm.userName}=".$username."&portlet_5_6{actionForm.password}=".$password;
$cookie = "cookie.txt" ;
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0");
curl_setopt($ch,CURLOPT_COOKIESESSION,false); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt ($ch, CURLOPT_REFERER, $referer);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch);
logIntoLocator(); 
curl_close($ch);

function logIntoLocator()
{
    $pincode = "731234";
    $locatorType = "P";
    $url = "http://onlinelic.in/LICEPS/appmanager/Customer/CustomerHome?_nfpb=true&_windowLabel=Cust_Agent_Locator_portlet_25_2&Cust_Agent_Locator_portlet_25_2_actionOverride=%2Fportlets%2Fvisitor%2FAgentLocator%2Flocating";
    $referer = "https://customer.onlinelic.in/LICEPS/appmanager/Customer/CustomerHome?_nfpb=true&_windowLabel=CustomerLocatorsPortlet_1&_cuid=RC_t_832059&_pagechange=AgentLocator";
    $postData = "Cust_Agent_Locator_portlet_25_2wlw-radio_button_group_key:{actionForm.agentRadioOption}=".$locatorType."&Cust_Agent_Locator_portlet_25_2{actionForm.agentOption}=".$pincode;
    $cookie = "cookie.txt" ;
    $agentCurl = curl_init();
    curl_setopt ($agentCurl, CURLOPT_URL, $referer); 
    curl_setopt ($agentCurl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt ($agentCurl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0");
    curl_setopt($agentCurl,CURLOPT_COOKIESESSION,true);
    curl_setopt ($agentCurl, CURLOPT_TIMEOUT, 60); 
    curl_setopt ($agentCurl, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt ($agentCurl, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt ($agentCurl, CURLOPT_REFERER, $referer);
    curl_setopt ($agentCurl, CURLOPT_COOKIEJAR, $cookie);
    curl_setopt ($agentCurl, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt ($agentCurl, CURLOPT_POSTFIELDS, $postData);
    curl_setopt ($agentCurl, CURLOPT_POST, 1); 
    $result = curl_exec ($agentCurl);    
    echo $result;   
}

If you like to give it a try the username is "manashch1" and the password is "nokia1105*". login there and go the AgentLocator and there u can enter pincode as 731234 and get the data needed.

  • 写回答

3条回答 默认 最新

  • douchuang4402 2012-06-28 11:47
    关注

    You need to attach the cookie(s) to CURL. There are two options:

    1. You can do this all manually by reading the cookie, and attaching manually using

      $ch = curl_init();
      $sCookie = 'JSESSIONID=' . $sessionIDSavedEarlier . '; path=/'; 
      curl_setopt ( $ch , CURLOPT_COOKIE, sCookie );
      ... rest of POST
      
    2. You can use a "cookie jar" (probably easiest)

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
      curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
      ... rest of POST
      

    (Making sure you can read/write to a file called "cookie.txt" - if you have multiple users using hte script, then make that file unique for each user through a PHP session!)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记