title: PHP post request with cURL and cookie
body: <p>I am trying to make request to a webpage which requires a login.
  I successfully take the cookie with the SESSID and write it to a file with curl:</p>
  
  <pre><code>$username = 'xxx';
  $password = 'xxxxxxx';
  $url = 'http://example.com';
  $cookie="cookie.txt";
  $postdata = "username=$username&amp;userpass=$password&amp;autologin=1&amp;userlogin=Login";
  
  $ch = curl_init();
  curl_setopt ($ch, CURLOPT_URL, $url);
  curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
  curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
  curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
  curl_setopt ($ch, CURLOPT_REFERER, $url);
  
  curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
  curl_setopt ($ch, CURLOPT_POST, 1);
  $result = curl_exec ($ch);
  
  curl_close($ch);
  </code></pre>
  
  <p>Now after this I want to make a POST request to the site with the cookie so it recognizes me as logged in. It uses API that returns JSON information if you do request like:
  <code>site.com/API/command.get(x)</code>  - just an example. So if you open this in the browser, it returns the JSON data.</p>
  
  <p>Can anyone help me with a way, how to do this? I am new to curl.</p>
  
  <p>No matter what I try, I get redirected to login page.
  Tried to pass <code>curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);</code>, no success.</p>