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

问题描述:

我正在尝试登录到网站,然后转到链接,然后提交表单以获取所需的数据。我想用cURL来做。我在登录网站方面取得了成功。登录将我重定向到配置文件页面。使用Curl登录网站,转到链接,再次提交表单并获得输出

现在我需要关注一个链接,然后提交表单!但是当我这样做使用CURL会话失效。我在一个cookie.txt中获取JSESSIONID我用来存储创建的cookie的文件。我所看到的所有例子都只是登录到一个网站或只是一个注册表submit.that只是一个POST请求!

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

我正在使用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并获取所需的数据。

您需要将Cookie附加到CURL。有两个选项:

  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”的文件 - 如果您有多个用户使用hte脚本,则通过PHP会话为每个用户创建该文件)

+0

使用过!没有运气!! jsessionid永远不会与请求头一起发送。现在我有一个非常奇怪的问题...我有Cookie文件jsessionid和请求标头显示Cookie:PHPSESSID = blahblahblah:O –

+1

感谢您的代码 - 看不到任何明显错误。建议:使用选项CURLOPT_COOKIESESSION来防止以前的会话变量被加载(或者删除/取消连接cookie.txt),关闭第一个curl会话(以防它在curl关闭之后不更新cookie jar文件) 。您可以使用file_m_date在调用第二个函数之前检查修改的日期来验证这一点。但这些都是尝试的项目,不保证结果! – Robbie

+0

但要进行会议,我需要使用以前的会议权?无论如何,他们都没有工作..该文件正在更改每次.. jsessionid在cookie文件内更改。 :( –

所以你想登录,获取页面,然后转到一个只有登录后才可访问的页面?

如果是这样,那么请确保cookie.txt可由脚本写入,并检查url是否包含任何特殊参数(如会话ID)。你是否从档案页面中提取它?

如果您知道表单的内容,您应该能够直接发布到该表单目标,但您可能需要将引用页面设置为登录到的网站。

+0

Cookie文件文件夹是可写的。并且没有特殊的参数像sessionid在url ... m在这里发布代码。 –

curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); 

在您的cUrl脚本中使用此文件。 凡

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

其中form1,form2 ..是输入的'name'属性。

+0

我知道..我已经完成了那个。检查code./。 。它已经5天了,它的痛苦在屁股..boss要它做 –

+0

也许curl_getinfo函数可能帮助 – user1485518

+0

也试图把curl_setopt($ curl,CURLOPT_HEADER,0);在curl初始化后。 SSL验证器设置为真? – user1485518