title: cURL works from Terminal, but not from PHP
body: <p>I'm running into a rather strange issue.</p>
  
  <p>I'm trying to log into a remote moodle install using curl from PHP.</p>
  
  <p>I have a curl command, which works perfectly in the Terminal.</p>
  
  <p>When I translate the same thing into PHP, it works, but it just doesn't login. The exact same value which successfully login via terminal, somehow trips up the login system via PHP and it doesn't login. Instead, it returns the login page again.</p>
  
  <p>My cURL command (data section ommitted as it has my username and password):</p>
  
  <pre><code>curl 'http://moodle.tsrs.org/login/index.php'
  -H 'Pragma: no-cache'
  -H 'Origin: http://moodle.tsrs.org'
  -H 'Accept-Encoding: gzip, deflate'
  -H 'Accept-Language: en-US,en;q=0.8'
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36'
  -H 'Content-Type: application/x-www-form-urlencoded'
  -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
  -H 'Cache-Control: no-cache'
  -H 'Referer: http://moodle.tsrs.org/login/index.php'
  -H 'Cookie: MoodleSession=ngcidh028m37gm8gbdfe07mvs7; MOODLEID_=%25F1%25CD%2519D%25B2k%25FE%251D%25EFH%25E5t%25B1%2503%258E; MoodleSessionTest=NhzaTNij6j; _ga=GA1.2.925953522.1416155774; _gat=1; __utmt=1; __utma=147409963.925953522.1416155774.1416642544.1416692798.3; __utmb=147409963.1.10.1416692798; __utmc=147409963; __utmz=147409963.1416155774.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)'
  -H 'Connection: keep-alive'
  </code></pre>
  
  <p>The corresponding PHP code:</p>
  
  <pre><code>function login() {
      $username = $_POST['username'];
      $password = $_POST['password'];
  
      if(!isset($_POST['username']) || !isset($_POST['password'])) {
          echo "No login data received";
          return;
      }
  
      $creq = curl_init();
  
      $data = array('username' =&gt; $username, 'password' =&gt; $password, 'testcookies'=&gt; '1');
  
      $headers = array('Pragma: no-cache', 'Origin: http://moodle.tsrs.org', 'Accept-Encoding: ', 'Accept-Language: en-US,en;q=0.8', 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36', 'Content-Type: application/x-www-form-urlencoded', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cache-Control: no-cache', 'Cookie: MoodleSession=ngcidh028m37gm8gbdfe07mvs7; MOODLEID_=%25F1%25CD%2519D%25B2k%25FE%251D%25EFH%25E5t%25B1%2503%258E; MoodleSessionTest=NhzaTNij6j; _ga=GA1.2.925953522.1416155774; _gat=1; __utmt=1; __utma=147409963.925953522.1416155774.1416642544.1416692798.3; __utmb=147409963.1.10.1416692798; __utmc=147409963; __utmz=147409963.1416155774.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)', 'Connection: keep-alive' );
          curl_setopt_array($creq, array(
          CURLOPT_URL =&gt; 'http://moodle.tsrs.org/login/index.php',
          CURLOPT_RETURNTRANSFER =&gt; true,
          CURLOPT_POST =&gt; true,
          CURLOPT_ENCODING =&gt; '',
          CURLINFO_HEADER_OUT =&gt; true,
          CURLOPT_POSTFIELDS =&gt; $data,
          CURLOPT_HTTPHEADER =&gt; $headers,
          CURLOPT_FOLLOWLOCATION =&gt; false
      ));
  
      $output = curl_exec($creq);
  
      echo print_r(curl_getinfo($creq));
  
      echo "\n" . $output . "\n";
  }
  </code></pre>
  
  <p>And the output of curlinfo:</p>
  
  <pre><code>Array
  (
      [url] =&gt; http://moodle.tsrs.org/login/index.php
      [content_type] =&gt; text/html; charset=utf-8
      [http_code] =&gt; 200
      [header_size] =&gt; 541
      [request_size] =&gt; 945
      [filetime] =&gt; -1
      [ssl_verify_result] =&gt; 0
      [redirect_count] =&gt; 0
      [total_time] =&gt; 1.462409
      [namelookup_time] =&gt; 0.002776
      [connect_time] =&gt; 0.330766
      [pretransfer_time] =&gt; 0.330779
      [size_upload] =&gt; 365
      [size_download] =&gt; 8758
      [speed_download] =&gt; 5988
      [speed_upload] =&gt; 249
      [download_content_length] =&gt; -1
      [upload_content_length] =&gt; 365
      [starttransfer_time] =&gt; 0.694866
      [redirect_time] =&gt; 0
      [certinfo] =&gt; Array
          (
          )
  
      [primary_ip] =&gt; 125.22.33.149
      [redirect_url] =&gt;
      [request_header] =&gt; POST /login/index.php HTTP/1.1
  Host: moodle.tsrs.org
  Pragma: no-cache
  Origin: http://moodle.tsrs.org
  Accept-Language: en-US,en;q=0.8
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  Cache-Control: no-cache
  Cookie: MoodleSession=ngcidh028m37gm8gbdfe07mvs7; MOODLEID_=%25F1%25CD%2519D%25B2k%25FE%251D%25EFH%25E5t%25B1%2503%258E; MoodleSessionTest=NhzaTNij6j; _ga=GA1.2.925953522.1416155774; _gat=1; __utmt=1; __utma=147409963.925953522.1416155774.1416642544.1416692798.3; __utmb=147409963.1.10.1416692798; __utmc=147409963; __utmz=147409963.1416155774.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
  Connection: keep-alive
  Content-Length: 365
  Expect: 100-continue
  Content-Type: application/x-www-form-urlencoded; boundary=----------------------------83564ee60d56
  
  
  )
  </code></pre>
  
  <p>Does anyone know any possible reason for this? I've tried swapping out the hard coded cookie with COOKIEFILE and COOKIEJAR, but it doesn't change anything.</p>