我正在尝试登录一个网站,然后转到一个链接,然后提交一个表格来获取所需的数据。我想使用 cURL 来做到这一点。我已经成功地登录了网站。登录会将我重定向到个人资料页面。
现在我需要点击一个链接,然后提交一个表格! 但是当我使用 CURL 执行此操作时,会话无效。我在 cookie 中获取 JSESSIONID.txt我用来存储创建的 cookie 的文件。我看到的所有例子都只是登录一个网站或只是提交注册表,这只是一个 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*"。 登录那里并转到代理定位器,在那里您可以输入 PIN 码作为731234并获取所需的数据。
您需要将 cookie 附加到 CURL。有两种选择:
-
您可以通过读取 cookie 并手动附加来手动完成所有这些操作
$ch = curl_init(); $sCookie = 'JSESSIONID=' . $sessionIDSavedEarlier . '; path=/'; curl_setopt ( $ch , CURLOPT_COOKIE, sCookie ); ... rest of POST
-
您可以使用"饼干罐"(可能是最简单的)
$ch = curl_init(); curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); ... rest of POST
(确保您可以读取/写入名为"cookie.txt"的文件 - 如果您有多个用户使用该脚本,则通过 PHP 会话使该文件对每个用户唯一!
所以你想登录,获取页面,然后转到一个只有在登录后才能访问的页面?
如果是这样,请确保 cookie.txt 可由您的脚本写入,并检查 url 不包含任何特殊参数(如您的会话 ID)。您是否正在从配置文件页面中提取它。
如果您知道表单的内容,您应该能够直接发布到该表单目标,但您可能需要将引用页面设置为您登录的网站。
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
在您的 cUrl 脚本中使用它。哪里
$postfields = 'form1=value1&form2=value2&form3=value3';
其中 form1,form2.. 是输入的"名称"属性。