title: Download moodle mainpage using cURL
body: <p>Hi I'm having some trouble with logging in and downloading a web page.
  What i'm trying to do is log on to my school's moodle site and save the main page in a string so i can use a parser to pick out certain information and build a gwt app.</p>
  
  <p>this is what I have so far:
  in moodleLogin.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  
      &lt;form name="lms" action="moodlephp.php" method="post"&gt;
      &lt;input type="text" name="username" id="username" /&gt;&lt;br/&gt;
      &lt;input type="password" name="password" id="password" /&gt;
      &lt;input type="hidden" name="domain" id="domain" value="students.ltu.edu.au" /&gt;
      &lt;input type="submit" name="Login" id="Login" /&gt;
      &lt;/form&gt;
      &lt;div id="test"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>in moodlephp.php</p>
  
  <pre><code>&lt;?php   
  
      $username = $_POST["username"];
      $password = $_POST["password"];
      $domain = $_POST["domain"];
  
      $postfields = array( "username" =&gt; $username, "password" =&gt; $password, "domain" =&gt; $domain );
      $working_folder = "\cookies";
  
      $cookiefile = tempnam($working_folder, "cookies"); 
      $urlbase = "https://www.latrobe.edu.au/lms";
      $agent = $_SERVER['HTTP_USER_AGENT'];
      $debug = "";
      $debughandle = "";
  
  
      // get session cookies to set up the Moodle interaction
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_USERAGENT, $agent);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
      curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
      curl_setopt($ch, CURLOPT_VERBOSE, $debug);
      if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle);
  
      curl_setopt($ch, CURLOPT_URL, $urlbase . "/login/index.php"); 
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      $result = curl_exec ($ch);
      curl_close ($ch);
  
  
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_USERAGENT, $agent);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
      curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
      if(!$cookiefile) echo('empty');
      else echo($cookiefile);
      curl_setopt($ch, CURLOPT_VERBOSE, $debug);
      if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle);
  
      curl_setopt($ch, CURLOPT_URL, $urlbase . '/login/index.php');
      curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields)); 
      curl_setopt($ch, CURLOPT_POST, 1); 
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
      curl_setopt($ch, CURLOPT_HEADER, 1);
      $result = curl_exec ($ch);
      curl_close ($ch); 
  
      if (!$result || !preg_match("/HTTP\/1.1 303 See Other/", $result))
      {
      unlink($cookiefile);
      header("HTTP/1.0 403 Forbidden");
      die("Username/password incorrect.\n");
  
      }
  
      // get session key
  
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_USERAGENT, $agent);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
      curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
      curl_setopt($ch, CURLOPT_VERBOSE, $debug);
      if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle);
  
      curl_setopt($ch, CURLOPT_URL, $urlbase . "/question/import.php?courseid=" . $courseid); 
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      $result = curl_exec ($ch);
      curl_close ($ch);
  
      if (!preg_match("/sesskey=(\w*)/", $result, $matches))
      {
      unlink($cookiefile);
      header("HTTP/1.0 500 Internal Server Error");
      die("Could not determine sesskey.\n");
      }
  
      $sesskey = $matches[1];
  
      echo $result;
  ?&gt;
  </code></pre>
  
  <p>Any help would be greatly appreciated. All I'm receiving at the moment is "Username/password incorrect."</p>
  
  <p>I've been trying to follow the code posted here -> <a href="http://moodle.org/mod/forum/discuss.php?d=153580" rel="nofollow">http://moodle.org/mod/forum/discuss.php?d=153580</a>
  Thanks in advance.</p>