title: Login to another site with CURL POST (PHP)
body: <p>There is a website provided by our client that displays data I need for a report.</p>
  
  <p>My client refused to build an API so I can retrieve the data I need as JSON, or even better provide read-only access to their DB.</p>
  
  <p>So I thought, I can create a script that runs in a sever, that goes to the site, log in, search for an HTML table tag and retrieve data as an array in a loop, right?</p>
  
  <p>Well, I do not know how to properly log in to the site using cURL.</p>
  
  <p>The login form is as simple as two text boxes and a submit button, the form points to itself and once submitted redirects to another PHP file in the same server, that actually displays the data I need.</p>
  
  <p>I search through the web and I did find a lot of great examples.</p>
  
  <p>But when I try it, it returns a Boolean "True", and that is it. </p>
  
  <pre><code>$url = 'https://www.potato.site/login/login_file.php';
  $fields = array(
      'username' =&gt; urlencode('user'),
      'password' =&gt; urlencode('password'),
      'submit' =&gt; urlencode('Login')
  );
  $fields_string ="";
  
  foreach($fields as $key=&gt;$value) { $fields_string .= $key.'='.$value.'&amp;'; }
  rtrim($fields_string, '&amp;');
  
  $ch = curl_init();
  
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_POST, count($fields));
  curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
  
  //execute post
  $result = curl_exec($ch);
  
  
  //close connection
  curl_close($ch);
  
  echo $result;
  </code></pre>
  
  <p>I'm probably missing something here, I will greatly appreciate any help.</p>
  
  <p>Thank you.</p>