3

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と入力して、必要なデータを取得できます。

4

3 に答える 3

2

CookieをCURLに添付する必要があります。2つのオプションがあります。

  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セッションを通じてそのファイルをユーザーごとに一意にします。)

于 2012-06-28T11:47:29.987 に答える
0

ログインしてページを取得し、ログインして初めてア​​クセスできるページに移動しますか?

その場合は、cookie.txtがスクリプトで書き込み可能であることを確認し、URLに特別なパラメーター(セッションIDなど)が含まれていないことを確認してください。プロフィールページから抽出していますか?

フォームの内容を知っている場合は、そのフォームの宛先に直接投稿できるはずですが、参照ページをログインしているサイトに設定する必要がある場合があります。

于 2012-06-28T11:47:05.307 に答える
0
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);

これをcUrlスクリプトで使用します。どこ

$ postfields ='form1 = value1&form2 = value2&form3 = value3';

ここで、form1、form2 ..は、入力の「名前」属性です。

于 2012-06-28T11:52:22.507 に答える