title: Scraping ASPX with cURL - results from subsequent pages
body: <p>I am building a web scraper using PHP curl methods to scrape results from an ASPX page. I successfully got it to scrape the first results page but the problem arises when I try to get the results from subsequent pages I get a runtime error.</p>
  
  <p>I looked through all the hidden fields and only found __VIEWSTATE and __EVENTVALIDATION and I update those values. I am not sure why I am getting this runtime error.</p>
  
  <pre><code>$url = "";
  
  $results_page = scrape($url,'',"","");
  
  preg_match('~&lt;input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" /&gt;~', $results_page, $viewstate);
  
  preg_match('~&lt;input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" /&gt;~', $results_page, $eventValidation);
  
  $viewstate = $viewstate[1];
  $eventValidation = $eventValidation[1];
  
  echo $results_page;
  sleep(rand(3,5)); 
  
   // get next results page 
  echo $results_page = scrape($url,'',$viewstate,$eventValidation);
  
  
  
  function scrape($url,$zip,$v1,$v2) {
  $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';
  $f = fopen('log.txt', 'w'); // file to write request header for debug purpose
  
  /**    Get __VIEWSTATE &amp; __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);
  if(!$v1){
  preg_match('~&lt;input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" /&gt;~', $html, $viewstate);
  preg_match('~&lt;input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" /&gt;~', $html, $eventValidation);
  $viewstate = $viewstate[1];
  $eventValidation = $eventValidation[1];
  }
  /** Start form submit process */
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  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['__EVENTTARGET'] = "";
  $postfields['__EVENTARGUMENT'] = "";
  if($v1){$viewstate = $v1;$eventValidation=$v2;}
  $postfields['__VIEWSTATE'] = $viewstate;
  $postfields['__EVENTVALIDATION'] = $eventValidation;
  $postfields['zip'] = $zip;
  $postfields['county'] = '';
  $postfields['POISearch'] = "agent";
  if($v1){
  $postfields['method'] = "query";
  $postfields['pager'] = "nextData";
  $postfields['searchType'] = "agent";}
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $ret = curl_exec($ch);
  curl_close($ch); 
  return $ret;
  }
  </code></pre>