使用Curl登录网站,转到链接,再次提交表单并获取输出 [英] Using Curl to login to a website, go to a link, again submit a form and get the output

查看:140
本文介绍了使用Curl登录网站,转到链接,再次提交表单并获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录网站,然后转到链接,然后提交表格以获取所需的数据.我想使用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.

现在,我需要点击链接,然后提交表格!但是当我使用CURL进行操作时,会话无效.我在用来存储创建的cookie的cookie.txt 文件中获得了 JSESSIONID.我看到的所有示例都只是登录网站或提交注册表单.这只是一个 POST 请求!

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!

成功登录并存储cookie后,如何转到另一个链接,然后使用curl提交另一个表单?

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

我将WAMP用作本地服务器.

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;   
}

如果您想尝试一下,用户名是"manashch1",密码是"nokia1105 *".在那儿登录并进入AgentLocator,然后在那儿您可以输入密码为731234并获取所需的数据.

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.

推荐答案

您需要将Cookie附加到CURL.有两种选择:

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

  1. 您可以通过阅读cookie并使用

  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

  • 您可以使用饼干罐"(可能最简单)

  • 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
    

  • (确保您可以读取/写入名为"cookie.txt"的文件-如果您有多个用户使用hte脚本,请通过PHP会话使该文件对于每个用户都是唯一的!)

    (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!)

    这篇关于使用Curl登录网站,转到链接,再次提交表单并获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆