简体   繁体   中英

PHP Curl obtain values of __VIEWSTATE and __EVENTVALIDATION from aspx page

Hi I have written the following script to login into a website. What I want to now do is perform a search on the page that we see after we login. Currently my script returns the page after you login. It has a form that has a search field and a button. I have also noticed that it uses __VIEWSTATE and __EVENTVALIDATION as part of its form. I am aware that the values for these two fields are not always going to be the same. So I wanted to know how I can retrieve these values from the form when I perform a search so that I can use them to post the form in my script. Here is the code I used to login:

    <?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);

?>

You best bet would probably be to use PHP's DOMDocument class to traverse the returned HTML and get what you are looking for. You could load the result string into DOMDocument then use getElementsByTagName or getElementById to get the nodes. The latter would be preferred if the input elements have id values.

Implementation would look something like:

// $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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM