Webサイトにログインし、リンクに移動してフォームを送信し、目的のデータを取得しようとしています。cURLを使ってやりたいです。私はウェブサイトへのログインに成功しました。ログインすると、プロファイルページにリダイレクトされます。
リンクをたどってフォームを送信する必要があります。しかし、CURLを使用してこれを行うと、セッションが無効になります。作成したCookieを保存するために使用したcookie.txtファイルでJSESSIONIDを取得します。私が見たすべての例は、Webサイトへのログイン、または登録フォームの送信に関するものです。これは、1つの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に移動すると、ピンコードを731234と入力して、必要なデータを取得できます。