title: C++ authentication against PHP with cURL
body: <p>Im trying to get my c++ app to authenticate user/pass against a web database. To do so, I have a simple html form for user/pass which triggers the authentication php script.</p>
  
  <p>Im having a hard time understanding cURL since im a complete noob on it.
  Right now, I'm able to send data to the html form, but i dont even know if im doing properly.</p>
  
  <p>Ideally, I would like you to teach me how to do it, and how to read a response. I mean, if the login goes right, how do i get c++ to know it?</p>
  
  <p>All the code i have:</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form method="post" action="check.php"&gt;
      &lt;input type="text" name="uname"/&gt;
      &lt;input type="password" name="password"/&gt;
      &lt;input type="submit"/&gt;
  &lt;/form&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  $fileDir = '/var/www/html/forums/';
  require($fileDir . '/src/XF.php');
  XF::start($fileDir);
  $app = \XF::setupAPP('XF\App');
  
  $username = $_POST['uname']; $password = $_POST['password'];
  
  $ip = $app-&gt;request-&gt;getIp();
  $loginService = $app-&gt;service('XF:User\Login', $username, $ip);
  
  $userValidate = $loginService-&gt;validate($password, $error);
  if(!$userValidate)
  {
      //Not good pass / user
      $data = ['validated' =&gt; false];
  }
  else $data = ['validated' =&gt; true];
  
  header('Content-type: application/json');
  echo json_encode($data);
  ?&gt;
  </code></pre>
  
  <p>C++</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;curl/curl.h&gt;
  
  using namespace std;
  
  int main()
  {
  char username[20];
  char password[25];
  
  cout &lt;&lt; "Username: ";
  cin &gt;&gt; username;
  cout &lt;&lt; "Password: ";
  cin &gt;&gt; password;
  
  /*------------------------------------------*/
  
  CURL *curl;
  CURLcode res;
  
  curl_global_init(CURL_GLOBAL_ALL);
  
  curl = curl_easy_init();
  if (curl) {
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
      curl_easy_setopt(curl, CURLOPT_URL, "https://localhost/index.hmtl");
      curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "uname=?&amp;password=?", username, password);
  
      res = curl_easy_perform(curl);
  
      if (res != CURLE_OK) {
          fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
      }
      curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
  }
  </code></pre>
  
  <p><strong>EDIT: WORKING SOLUTION</strong></p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include &lt;curl/curl.h&gt;
  
  using namespace std;
  
  string urlencode(const string &amp;str) {
  char *escaped = curl_escape(str.c_str(), str.length());
  if (escaped == NULL) throw runtime_error("curl_escape failed!");
  
  string ret = escaped;
  curl_free(escaped);
  return ret;
  }
  
  size_t my_write_function(const void * indata, const size_t size, const    size_t count, void *out) {
  (*(string*)out).append((const char*)indata, size*count);
  return size * count;
  }
  
  int main()
  {
  string username;
  string password;
  
  cout &lt;&lt; "Username: ";
  getline(cin, username);
  cout &lt;&lt; "Password: ";
  getline(cin, password);
  
  /*------------------------------------------*/
  
  CURL *curl;
  CURLcode res;
  
  curl_global_init(CURL_GLOBAL_ALL);
  
  curl = curl_easy_init();
  if (curl) {
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
      curl_easy_setopt(curl, CURLOPT_URL, "https://urlto/index.html");
      curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, string("uname=" + urlencode(username) + "&amp;password=" + urlencode(password)).c_str());
  
      string response;
      curl_easy_setopt(curl, CURLOPT_URL, "https://urlto/check.php");
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_function);
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, &amp;response);
      res = curl_easy_perform(curl);
  
      //cout &lt;&lt; "Response from website: " &lt;&lt; response &lt;&lt; endl;
      if (response.find("true") == string::npos) {
          cout &lt;&lt; "Failed to login";
      }
      else cout &lt;&lt; "Log in successful";
  
      if (res != CURLE_OK) {
          fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
      }
      curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
  }
  </code></pre>