使用卷曲登录到一个网站,去链接,再次提交一个表单并获取输出(Using Curl to login

2019-07-30 04:15发布

我试图登录到网站,然后去一个链接,然后提交一个表单,以获得所需的数据。 我想用卷曲做到这一点。 我已经实现了在洛成功进入该网站。 登录重定向我的个人资料页。

现在我需要遵循一个链接,然后提交表单! 但是当我做到这一点使用curl会话无效。 我得到的JSESSIONID在我用来存储创建cookie的一个cookie.txt文件。 我已经看到了所有的例子都只是洛成一个网站,或只是一个登记表submission.that只是一个单一的POST请求!

我该如何去到另一条链路,然后提交使用curl后,我已经成功登录并存储在cookie的另一种形式?

我使用WAMP作为我的本地服务器。

<?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和有ü可以输入PIN代码为731234,并获得所需的数据。

Answer 1:

您需要附上保存cookie卷曲。 有两个选项:

  1. 您可以通过阅读该cookie,并手动安装使用所有手动执行此操作

     $ch = curl_init(); $sCookie = 'JSESSIONID=' . $sessionIDSavedEarlier . '; path=/'; curl_setopt ( $ch , CURLOPT_COOKIE, sCookie ); ... rest of POST 
  2. 您可以使用“饼干罐”(可能比较容易)

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

(确保你可以读/写一个名为“cookie.txt”文件 - !如果你有使用兴田脚本多个用户,然后作出独特的通过PHP会话的每个用户该文件)



Answer 2:

所以,你要登录,获取页面,然后后藤一个网页,其中仅在访问一旦登录?

如果是这样,那么确保cookie.txt是你写的脚本,并检查URL不包含任何特殊的参数(如会话ID)。 你是从个人资料页面中提取它。

如果你知道表单的内容,你应该能够发布到直接形成的目的地,但你可能需要引用页日志记录设置的网站进入。



Answer 3:

curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

在cUrl作者脚本中使用此。 哪里

$ postfields = 'Form1中=值1&窗口2 =值2&form3 =值3';

其中Form1中,窗口2 ..是你输入的“名称”属性。



文章来源: Using Curl to login to a website, go to a link, again submit a form and get the output