简体   繁体   中英

Login to remote site via php curl

Login URL is: https://192.XXX/abc/Login.aspx?FromMasterLogin=true

Header data sent by login page:

__EVENTTARGET:btnLogin __EVENTARGUMENT: __VIEWSTATE:/wEPDwULLTEwNzI1MzU5MzBkGAIFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYGBQhidG5Mb2dpbgUPYnRuQ2xlYXJTZXNzaW9uBRFSYWRXaW5kb3dNYW5hZ2VyMQUOcmR3aW5kb3dGb3JnZXQFD3Jkd2luZG93RW5mb3JjZQUYcmRXaW5kb3dQdWJsaWNOZXdzQWxlcnRzBQpyYWRDYXB0Y2hhDxQrAAIFJDQyM2FlNDE3LTEwMTctNDE2OS1hNjgzLTBmMjMyZDZkMDdmZAYAAAAAAAAAAGQdkHIfEfL2XAG+8+/wu30lMfjmEwOeIiiC7jveX5PnZg== __EVENTVALIDATION:/wEdAAfBlkUqNKBEV3moC9pS8IJTY3plgk0YBAefRz3MyBlTcJxpWckI3qdmfEJVCu2f5cGinihG6d/Xh3PZm3b5AoMQf2Dr69OxAarGhVFbQWZWFpd+ecw9lQ5sg8SY03yGmgNKhPS/+yQ5+zLwEb8uDfAwYKkBfoLWkbIJoPnHfXTqz5B/GZyy44ThZCPCAskCEVA= txtUserName:admin txtUserName_ClientState:{"enabled":true,"emptyMessage":""} txtpassword:admin@123 txtpassword_ClientState:{"enabled":true,"emptyMessage":""} btnLogin_ClientState: btnClearSession_ClientState: rdwindowForget_ClientState: rdwindowEnforce_ClientState: rdWindowPublicNewsAlerts_ClientState: RadWindowManager1_ClientState:

Below is the code

<?php
//username and password of account
$username = 'admin';
$password = 'admin@123';


//login form action url
$url="https://192.168.X.X/abc/Login.aspx?FromMasterLogin=true"; 
$postinfo ='txtUserName:admin&txtpassword:admin@123';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_exec($ch);

//page with the content I want to grab
//curl_setopt($ch, CURLOPT_URL, "https://192.168.X.X/abc/MemberManagement/MemberFileDownload.aspx");
//do stuff with the info with DomDocument() etc
$html = curl_exec($ch);
echo $html;
curl_close($ch);
?>

But the result says session expired.

I realize this doesn't answer your specific question about curl, but it might be worth trying the Guzzle HTTP client for PHP, which includes some functionality for maintaining sessions and reduces the confusion of working with curl directly.

Here's a snippet from the Guzzle documentation :

// Use a specific cookie jar
$jar = new \GuzzleHttp\Cookie\CookieJar;
$r = $client->request('GET', 'http://httpbin.org/cookies', [
    'cookies' => $jar
]);

// Use a shared client cookie jar
$client = new \GuzzleHttp\Client(['cookies' => true]);
$r = $client->request('GET', 'http://httpbin.org/cookies');

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