Skip to content

Instantly share code, notes, and snippets.

@akeinhell
Last active August 29, 2015 14:17
Show Gist options
  • Save akeinhell/52d2f1b656e1974599ed to your computer and use it in GitHub Desktop.
Save akeinhell/52d2f1b656e1974599ed to your computer and use it in GitHub Desktop.
CPP API WoT
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "curl/curl.h"
//#pragma comment(lib,"curllib.lib") // для VC++
//--------------------------------------------------------------------------
const size_t BUF_SIZE= 5000000;
char wr_buf[5000001];
size_t wr_index=0;
//---------------------------------------------------------------------------
static size_t write_data(char *ptr, size_t size, size_t nmemb, char* data)
{
if(data==NULL || wr_index + size*nmemb > BUF_SIZE) return 0; // Если выход за размеры буфера - ошибка
memcpy( &data[wr_index], ptr, size*nmemb);// дописываем данные в конец
wr_index+= size*nmemb; // изменяем текущее положение
return size*nmemb;
}
char* getUrl(char* url)
{
printf("GetUrl: %s\n", url);
CURL *curl_handle;
CURLcode res;
memset(wr_buf,BUF_SIZE+1,0); // заполняем буфер нулями.
curl_handle = curl_easy_init();
if(curl_handle)
{
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, wr_buf);
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
CURLcode res = curl_easy_perform(curl_handle);
if(res == CURLE_OK)
{
return(wr_buf); // выводим буфер в консоль.
}
else
return( (char *)curl_easy_strerror(res) );
curl_easy_cleanup(curl_handle);
}
}
char* api(char* method, int id)
{
char url[200];
sprintf(url, "http://api.worldoftanks.ru/wot/%s/?application_id=demo&account_id=%d", method, id);
return getUrl(url);
}
//---------------------------------------------------------------------------
int main()
{
// system("clear");
char* url = "http://httpbin.org/ip";
printf("API: %s\n", api("account/info", 666));
return 0;
}
//------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment