Skip to content

Instantly share code, notes, and snippets.

@wwarodom
Last active August 19, 2016 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwarodom/3c9d6ae9370fcd1741e502986ff10b12 to your computer and use it in GitHub Desktop.
Save wwarodom/3c9d6ae9370fcd1741e502986ff10b12 to your computer and use it in GitHub Desktop.
sis PSU Phuket (Login)
<?php
require_once("simple_html_dom.php");
ini_set('display_errors', 'On');
error_reporting(E_ALL);
function get($url,$cookieFile, $referer, $params=array()) {
$url = $url.'?'.http_build_query($params, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setOpt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Host: sis-phuket1.psu.ac.th'));
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function post($url,$cookieFile, $referer, $fields){
$post_field_string = http_build_query($fields, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setOpt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Origin: https://sis-phuket1.psu.ac.th', 'Host: sis-phuket1.psu.ac.th'));
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// for POST method
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
curl_close ($ch);
return $response;
}
$curl_scraped_page = get('https://sis-phuket1.psu.ac.th/WebRegist2005/Login.aspx','cookie.txt','',array('no'=>' '));
$html = str_get_html($curl_scraped_page);
$viewState = $html->find("#__VIEWSTATE")[0]->attr['value'];
$viewStateGenerator = $html->find("#__VIEWSTATEGENERATOR")[0]->attr['value'];
$result = post('https://sis-phuket1.psu.ac.th/WebRegist2005/Login.aspx',
'cookie.txt',
'https://sis-phuket1.psu.ac.th/WebRegist2005/Login.aspx',
array(
'__LASTFOCUS'=>'',
'__EVENTTARGET'=> 'ctl00$mainContent$Login1$LoginButton',
'__EVENTARGUMENT' => '',
'__VIEWSTATE' => $viewState,
'__VIEWSTATEGENERATOR' => $viewStateGenerator,
'ctl00$mainContent$Login1$UserName'=>'psuUserName',
'ctl00$mainContent$Login1$Password'=>'psuPassword'
)
);
echo $result;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment