1

こんにちは私はウェブサイトにログインするために次のスクリプトを書きました。私が今やりたいのは、ログイン後に表示されるページで検索を実行することです。現在、私のスクリプトはログイン後にページを返します。検索フィールドとボタンのあるフォームがあります。また、フォームの一部として__VIEWSTATEと__EVENTVALIDATIONを使用していることにも気づきました。これら2つのフィールドの値が常に同じになるとは限らないことを認識しています。そこで、検索を実行するときにフォームからこれらの値を取得して、スクリプトにフォームを投稿できるようにする方法を知りたいと思いました。ログインに使用したコードは次のとおりです。

    <?php

$post_data['ctl00$MainContent$EmailText'] = 'xxxx@xxxx.com';
$post_data['ctl00$MainContent$PasswordText'] = 'xxxx';
$post_data['ctl00$MainContent$LogInButton'] = 'Log On';

foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

$post_string = implode ('&', $post_items);

$curl_connection =
  curl_init('https://www.XXXX.co.uk/Login.aspx');
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_connection, CURLOPT_COOKIEJAR, $ckfile); 

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

$result = curl_exec($curl_connection);

curl_close($curl_connection);

?>
4

1 に答える 1

1

おそらく、PHP の DOMDocument クラスを使用して、返された HTML をトラバースし、探しているものを取得することをお勧めします。結果の文字列を DOMDocument にロードしてから、 getElementsByTagNameorgetElementByIdを使用してノードを取得できます。入力要素に id 値がある場合は、後者が優先されます。

実装は次のようになります。

// $result is string returned by cURL from your code
$dom = new DOMDocument();
$dom->loadHTML($result);
$node = $dom->getElementById('your_element_id');
$node_value = $node->getAttribute('value');
于 2012-09-17T15:11:25.173 に答える