VIEWSTATE
[clinton/MarylandElectronicPetitionSignature.git] / api / maryland_voter.php
1 <?PHP
2 ini_set('display_errors', 1);
3 ini_set('display_startup_errors', 1);
4 error_reporting(E_ALL);
5
6 function cut_part_out($start,$end,$whole){
7 $parts = explode($start,$whole);
8 $subparts = explode($end,$parts[1]);
9 $out = $subparts[0];
10 return $out;
11 }
12
13 function getPage($url,$cookie=''){
14 $url = str_replace('[month]',date('F'),$url); // replace month January through December
15 $url = str_replace('[day]',date('j'),$url); // replace day 1 to 31
16 $url = str_replace('[yesterday]',date('j',strtotime('yesterday')),$url); // replace day 1 to 31
17 $url = str_replace('[year]',date('Y'),$url); // replace year Examples: 1999 or 2003
18 $curl = curl_init();
19 curl_setopt ($curl, CURLOPT_URL, $url);
20 curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("McGuire MEPS https://www.md-petition.com/ /%d.0",rand(4,50)));
21 curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
22 curl_setopt ($curl, CURLOPT_HEADER, 1);
23 curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
24 curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true);
25 if ($cookie != ''){
26 curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Cookie: ASP.NET_SessionId=$cookie")); // use cookies
27 }
28 /*
29 $postfields = array(
30 'upload_file' => '@file_to_upload.png',
31 'upload_text' => '@text_to_upload'
32 );
33 curl_setopt ($curl, CURLOPT_POSTFIELDS, $postFields);
34 */
35 $html = curl_exec ($curl);
36 curl_close ($curl);
37 return $html;
38 }
39
40 // start a session to get a cookie
41 $form['url'] = 'https://voterservices.elections.maryland.gov/VoterSearch';
42 $form['html'] = getPage($form['url']);
43
44 // extract the cookie from the header (see CURLOPT_HEADER)
45 $cookie = cut_part_out('ASP.NET_SessionId=',';',$form['html']);
46 $VIEWSTATE = cut_part_out('id="__VIEWSTATE" value="','"',$form['html']);
47 // Found Form Elements
48 //__VIEWSTATE
49 //__VIEWSTATEGENERATOR
50 //__VIEWSTATEENCRYPTED
51 //__EVENTVALIDATION
52 //ctl00$MainContent$listLanguages = en
53
54 // debug - show full response make sure we have the cookie
55 echo "<li>COOKIE ASP.NET_SessionId $cookie</li>";
56 echo "<li>POST VIEWSTATE $VIEWSTATE</li>";
57
58 echo htmlspecialchars($form['html']);
59 echo '<hr><h1>Rendered</h1>';
60 echo $form['html'];