#include "C:\Program Files (x86)\CodeBlocks\curl-7.50.3-win32\include\curl\curl.h"
#include <stdio.h>
#include <iostream>

size_t WriteMemoryCallback(void *, size_t, size_t, void *);

using namespace std;

string ctxt;
int main(void)
{
  CURL *curl;
  CURLcode res;
  FILE *stream;
  struct curl_slist *listheader = NULL;


  stream=fopen((char*)"html.txt","w+b");

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.fr/search?num=30&q=meteo+boulogne-billancourt&ie=utf-8&oe=utf-8&aq=t&gws_rd=ssl");
    curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
    //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    //curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    //curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);

    listheader = curl_slist_append(listheader, "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*");
    //listheader = curl_slist_append(listheader, "Accept-Language: en-us,en;q=0.5");
    listheader = curl_slist_append(listheader, "Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3");
    listheader = curl_slist_append(listheader, "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)");
    listheader = curl_slist_append(listheader, "Connection: Keep-Alive");
    //listheader = curl_slist_append(listheader, "Content-Type: text/html; charset-UTF-8");
    //curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 6L);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 8L);
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, listheader);
    curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    //curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)stream);
    res = curl_easy_perform(curl);
    //cout<<"coucou";
    if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

    curl_easy_cleanup(curl);
    fwrite(ctxt.data(),ctxt.length(),1,stream);
    fclose(stream);
  }
  return 0;
}
size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
  ctxt.append((char *)contents);
  //MessageBox(hwnd,inttostr((int)ctxt.data()).c_str(), "Look", MB_OK);
  return size * nmemb;
}
