Php 使用Curl登录网站,转到链接,再次提交表单并获得输出
我试图登录到一个网站,然后去一个链接,然后提交一个表格,以获得所需的数据。我想用卷曲来做。我成功地登录了这个网站。登录会将我重定向到配置文件页面 现在我需要遵循一个链接,然后提交一份表格!但是,当我使用CURL执行此操作时,会话无效。我在用于存储创建的cookie的cookie.txt文件中获取JSESSIONID。我看到的所有例子都是关于登录一个网站或提交一份注册表格。这只是一个POST请求 在成功登录并存储cookie后,如何转到另一个链接,然后使用curl提交另一个表单 我正在使用WAMP作为本地服务器Php 使用Curl登录网站,转到链接,再次提交表单并获得输出,php,forms,cookies,curl,login,Php,Forms,Cookies,Curl,Login,我试图登录到一个网站,然后去一个链接,然后提交一个表格,以获得所需的数据。我想用卷曲来做。我成功地登录了这个网站。登录会将我重定向到配置文件页面 现在我需要遵循一个链接,然后提交一份表格!但是,当我使用CURL执行此操作时,会话无效。我在用于存储创建的cookie的cookie.txt文件中获取JSESSIONID。我看到的所有例子都是关于登录一个网站或提交一份注册表格。这只是一个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;
}
那么您想登录,获取页面,然后转到一个只有登录后才能访问的页面
如果是这样,那么请确保脚本可以写入cookie.txt,并检查url不包含任何特殊参数(如会话ID)。您正在从配置文件页面提取它
如果您知道表单的内容,您应该能够直接发布到表单目标,但是您可能需要将引用页面设置为您登录的站点。您需要将cookie附加到CURL。有两种选择:
您可以通过读取cookie并使用
$ch = curl_init();
$sCookie = 'JSESSIONID=' . $sessionIDSavedEarlier . '; path=/';
curl_setopt ( $ch , CURLOPT_COOKIE, sCookie );
... rest of POST
您可以使用“饼干罐”(可能最简单)
(确保您可以读取/写入名为“cookie.txt”的文件-如果您有多个用户使用hte脚本,则通过PHP会话使该文件对每个用户都是唯一的!)
在cUrl脚本中使用此选项。
在哪里
$postfields='form1=value1&form2=value2&form3=value3'
其中,form1,form2。。是您输入的“名称”属性。使用该属性!不走运!!JSSessionID永远不会与请求头一起发送。现在我遇到了一个非常奇怪的问题…我在cookie文件中有jsessionid,请求头显示cookie:PHPSESSID=blahblahblah:OThanks作为代码-看不出任何明显的错误。建议:使用选项CURLOPT_COOKIESESSION以防止加载以前的会话变量(或者确保删除/取消链接cookie.txt),关闭第一个curl会话(以防它在关闭curl之后才更新cookie jar文件)。在调用第二个函数之前,可以使用file_m_date检查修改的日期来验证这一点。但这些都是需要尝试的项目,不能保证结果!但是为了进行这个环节,我需要使用上一个环节,对吗???无论如何,它们都不起作用..每次都会更改文件..cookie文件中的jsessionid会更改:(暂时离开最后一部分..我怎么能在登录后直接转到配置文件页面内的链接。甚至重定向都说会话无效。啊-我想你的代码中的CURLOPT_cookies会话是错误的。从true开始(在第一部分中,忽略上一个会话cookie)和false(或在后续页面中省略)。cookie文件夹是可写的..url中没有sessionid这样的特殊参数…我在这里发布代码。我知道..我已经这样做了..检查代码。/…已经5天了,这让人很痛苦..老板希望这样做可能curl\u getinfo函数也可以尝试放置curl\u setopt($curl,CURLOPT_HEADER,0);在curl init之后启用。是否确实要将ssl验证器设置为true?该站点使用https,因此我将其设置为true。我将其设置为..它为我提供JSESSIONID..与存储在cookie.txt中的id相同
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
... rest of POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);