I want to login to a remote website from mine. So, I used cURL to achieve this and I was successful using the code below:
function login($url,$data)
{
$fp = fopen("cookie.txt", "w");
fclose($fp);
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($login, CURLOPT_TIMEOUT, 40000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($login, CURLOPT_POST, TRUE);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
ob_start();
return curl_exec ($login);
ob_end_clean();
curl_close ($login);
unset($login);
}
So for posting data I used the raw data for the form in the website. Which I managed to get from developer tools in Edge. Here is the screenshot of raw POST data when I try to login directly in the remote website ( http://vce.ac.in )
The problem is the values of the parameters viewstate, validation, viewstategenerator are changing for every 24 hours.
So if there is a method in PHP to get the raw POST data of the website, I can update those values of raw data parameters which are changing.
Parse them with a simple PHP DOM
// get simple_html_dom.php from : https://netix.dl.sourceforge.net/project/simplehtmldom/simple_html_dom.php
require_once('simple_html_dom.php');
function login($url, $data){
$html = file_get_html( $url );
foreach ($html->find('input') as $input){
# echo "INPUTDOM". print_r($input);
if ($input->attr['name']=="__VIEWSTATE"){
//__VIEWSTATE
echo "__VIEWSTATE: {$input->attr['value']}\n";
$form_data['__VIEWSTATE'] = $input->attr['value'];
} elseif ($input->attr['name']=="__VIEWSTATEGENERATOR"){
//__VIEWSTATEGENERATOR
echo "__VIEWSTATEGENERATOR: {$input->attr['value']}\n";
$form_data['__VIEWSTATEGENERATOR'] = $input->attr['value'];
} elseif ($input->attr['name']=="__VIEWSTATE"){
//__EVENTVALIDATION
echo "__VIEWSTATE: {$input->attr['value']}\n";
$form_data['__EVENTVALIDATION'] = $input->attr['value'];
}
};
return $form_data;
}
echo login('http://vce.ac.in/index.aspx',['txtLoginID'=> 'user', 'txtPWD'=> 'password'] );
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.