title: PHP: Login with curl to aspx
body: <p>I'm trying to login to one page programmed in ASP.NET (Pages of my school, they haven't mobile version and it's very bad on mobile, so i'm trying load any information to my web.)</p>
  
  <p>Login is OK, but the page doesn't load whole. Only header.</p>
  
  <p>I haven't idea why.</p>
  
  <p>There is script (it's from <a href="http://www.mishainthecloud.com/" rel="nofollow">http://www.mishainthecloud.com/</a>):</p>
  
  <pre><code>    &lt;?php
  // urls to call - the login page and the secured page
  $urlLogin = "http://bakalari.gymtce.cz/bakaweb/login.aspx";
  $urlSecuredPage = "http://bakalari.gymtce.cz/bakaweb/prehled.aspx?s=2";
  
  // POST names and values to support login
  $nameUsername='ctl00$cphmain$TextBoxjmeno';       // the name of the username textbox on the login form
  $namePassword='ctl00$cphmain$TextBoxHeslo';       // the name of the password textbox on the login form
  $nameLoginBtn='ctl00$cphmain$ButtonPrihlas';          // the name of the login button (submit) on the login form
  $valUsername ='xxx';        // the value to submit for the username
  $valPassword ='xxx';        // the value to submit for the password
  $valLoginBtn ='';             // the text value of the login button itself
  
  // the path to a file we can read/write; this will
  // store cookies we need for accessing secured pages
  $cookies = 'cookie.txt';
  
  // regular expressions to parse out the special ASP.NET
  // values for __VIEWSTATE and __EVENTVALIDATION
  $regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
  $regexEventVal  = '/__EVENTVALIDATION\" value=\"(.*)\"/i';
  
  
  /************************************************
  * utility function: regexExtract
  *    use the given regular expression to extract
  *    a value from the given text;  $regs will
  *    be set to an array of all group values
  *    (assuming a match) and the nthValue item
  *    from the array is returned as a string
  ************************************************/
  function regexExtract($text, $regex, $regs, $nthValue)
  {
  if (preg_match($regex, $text, $regs)) {
   $result = $regs[$nthValue];
  }
  else {
   $result = "";
  }
  return $result;
  }
  
  
  
  /************************************************
  * initialize a curl handle; we'll use this
  *   handle throughout the script
  ************************************************/
  $ch = curl_init();
  
  
  /************************************************
  * first, issue a GET call to the ASP.NET login
  *   page.  This is necessary to retrieve the
  *   __VIEWSTATE and __EVENTVALIDATION values
  *   that the server issues
  ************************************************/
  curl_setopt($ch, CURLOPT_URL, $urlLogin);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  $data=curl_exec($ch);
  
  // from the returned html, parse out the __VIEWSTATE and
  // __EVENTVALIDATION values
  $viewstate = regexExtract($data,$regexViewstate,$regs,1);
  $eventval = regexExtract($data, $regexEventVal,$regs,1);
  
  
  /************************************************
  * now issue a second call to the Login page;
  *   this time, it will be a POST; we'll send back
  *   as post data the __VIEWSTATE and __EVENTVALIDATION
  *   values the server previously sent us, as well as the
  *   username/password.  We'll also set up a cookie
  *   jar to retrieve the authentication cookie that
  *   the server will generate and send us upon login.
  ************************************************/
  $postData = '__VIEWSTATE='.rawurlencode($viewstate)
            .'&amp;__EVENTVALIDATION='.rawurlencode($eventval)
            .'&amp;'.$nameUsername.'='.$valUsername
            .'&amp;'.$namePassword.'='.$valPassword
            .'&amp;'.$nameLoginBtn.'='.$valLoginBtn
            ;
  
  curl_setOpt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  curl_setopt($ch, CURLOPT_URL, $urlLogin);   
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);     
  
  $data = curl_exec($ch);
  
  
  /************************************************
  * with the authentication cookie in the jar,
  * we'll now issue a GET to the secured page;
  * we set curl's COOKIEFILE option to the same
  * file we used for the jar before to ensure the
  * authentication cookie is sent back to the
  * server
  ************************************************/
  curl_setOpt($ch, CURLOPT_POST, FALSE);
  curl_setopt($ch, CURLOPT_URL, $urlSecuredPage);   
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);     
  
  $data = curl_exec($ch);
  
  // at this point the secured page may be parsed for
  // values, or additional POSTS made to submit parameters
  // and retrieve data.  For this sample, we'll just
  // echo the results.
  echo $data;
  
  
  
  /************************************************
  * that's it! Close the curl handle
  ************************************************/
  curl_close($ch);
  
  
  ?&gt;
  </code></pre>
  
  <p>I did not put the login code is too long and confusing.. It's available on the URL from the script .. </p>
  
  <p>But i think, that the bug is maybe because of one script from secured page </p>
  
  <p>This:</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
  //&lt;![CDATA[
  var theForm = document.forms['formbaka'];
  if (!theForm) {
      theForm = document.formbaka;
  }
  function __doPostBack(eventTarget, eventArgument) {
      if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
          theForm.__EVENTTARGET.value = eventTarget;
          theForm.__EVENTARGUMENT.value = eventArgument;
          theForm.submit();
      }
  }
  //]]&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>Thank you and sorry for my english.</p>
  
  <p>//</p>
  
  <p>I edit part of code like this </p>
  
  <pre><code>curl_setopt($ch, CURLOPT_URL, $urlLogin);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
  $data=curl_exec($ch);
  
  // from the returned html, parse out the __VIEWSTATE and
  // __EVENTVALIDATION values
  $viewstate = regexExtract($data,$regexViewstate,$regs,1);
  $eventval = regexExtract($data, $regexEventVal,$regs,1);
  
  
  /************************************************
  * now issue a second call to the Login page;
  *   this time, it will be a POST; we'll send back
  *   as post data the __VIEWSTATE and __EVENTVALIDATION
  *   values the server previously sent us, as well as the
  *   username/password.  We'll also set up a cookie
  *   jar to retrieve the authentication cookie that
  *   the server will generate and send us upon login.
  ************************************************/
  $postData = '__LASTFOCUS='
            .'&amp;__EVENTTARGET='
            .'&amp;__EVENTARGUMENT='
            .'&amp;__VIEWSTATE='.rawurlencode($viewstate) 
            .'&amp;'.$nameUsername.'='.$valUsername
            .'&amp;'.$namePassword.'='.$valPassword
            .'&amp;'.$nameLoginBtn.'='.$valLoginBtn
            .'&amp;popupWS=0%3A0%3A-1%3A-10000%3A-10000%3A0%3A200px%3A100px%3A1%3A0%3A0%3A0'
            .'&amp;ctl00%24popup%24checklexpredmety=U'     
            .'&amp;DXScript=1_145%2C1_81%2C1_137%2C1_92%2C1_78%2C1_130%2C1_128%2C1_99%2C1_95'
  
            ;
  </code></pre>
  
  <p>Here is image with POST from official app - <a href="http://holicz.eu/content/images/post.JPG" rel="nofollow">link</a></p>