1

I logged in to a site written in ASP using PHP-CURL. After logging in I need to pull data from the page. The datas is updated with a element on the page. How do I make the selection by posting "option value" using Curl?

Select element:

 </select>
 <span id="ctl02_lblDonem" class="NormalBold">DÖNEM</span>
 <select name="ctl02$dlDonem" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl02$dlDonem\&#39;,\&#39;\&#39;)&#39;, 0)" id="ctl02_dlDonem" class="NormalBlack">
      <option value="-10">G&#220;Z</option>
      <option value="-15">G&#220;Z SONU EK D&#214;NEM</option>
      <option selected="selected" value="-20">BAHAR</option>
      <option value="-25">BAHAR SONU EK D&#214;NEM</option>
      <option value="-30">YAZ</option>
      <option value="-35">YAZ SONU EK D&#214;NEM</option>
 </select>

Script:

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['Form1'];
if (!theForm) {
    theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

My Code :

<?php
$url = "https://site/Default.aspx";
$ckfile = tempnam("/tmp", "CURLCOOKIE");
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2';

$username = "xxxx";
$password = "xxxx";


$f = fopen('log.txt', 'w'); // file to write request header for debug purpose

/**
    Get __VIEWSTATE & __EVENTVALIDATION
 */
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

$html = curl_exec($ch);

curl_close($ch);

preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate);
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation);
preg_match('~<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="(.*?)" />~', $html, $eventGenerator);

$viewstate = $viewstate[1];
$eventValidation = $eventValidation[1];
$viewGenerator =  $eventGenerator[1];



/**
 Start Login process
 */
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

// Collecting all POST fields
$postfields = array();
$postfields['tcmDefault_HiddenField'] = "";
$postfields['__LASTFOCUS'] = "";
$postfields['__EVENTTARGET'] = "";
$postfields['__EVENTARGUMENT'] = "";
$postfields['__VIEWSTATE'] = $viewstate;
$postfields['__VIEWSTATEGENERATOR'] = $viewGenerator;
$postfields['__EVENTVALIDATION'] = $eventValidation;
$postfields['ctl02$txtboxOgrenciNo'] = $username;
$postfields['ctl02$txtBoxSifre'] = $password;
$postfields['ctl02$btnLogin'] = "Giriş";

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch); // Get result after login page.
curl_setopt($ch, CURLOPT_URL, 'https://site/Default.aspx?tabInd=2&tabNo=3');  //select element inside this page.

//execute the request
$content = curl_exec($ch);

echo $content;
?>

Thanks

Elon Than
  • 9,603
  • 4
  • 27
  • 37
mrdeveloper
  • 55
  • 1
  • 7

1 Answers1

1

Simplest way is open Chrome DevTools (F12), navigate to network tab, manually do the nessesary actions and then put this data to CURL.

More information can be found here

Community
  • 1
  • 1
Alexey Shatrov
  • 450
  • 4
  • 12