[ad_1]
I wanna do first a call using cURL into a JSON and save the respond buffer body in array/memory and then to be able to loop thought this array/memory and get each saved line, then to do my comparisons, i tried to follow https://curl.se/libcurl/c/getinmemory.html cURL code example but im stuck in how to loop thought the saved ‘chunk.memory’ buffer to get each saved line, any help is appreciated.
I wanna do the loop under else { config_status("Received data: %*\n", chunk.memory); }
where chunk.memory
supposed to be the memory buffer there.
NOTE: I am a C beginner 😛
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if (!ptr) {
/* out of memory! */
//printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int check_ip(char *ip) {
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1);
chunk.size = 0;
char *url = "http://proxycheck.io/v2/204.225.96.123?key=10i9s3-685432-3f2774-t14562&vpn=1&asn=1&risk=1&port=1&days=7";
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:100.0) Gecko/20100101 Firefox/100.0");
/*
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "length: 20");
headers = curl_slist_append(headers, "numbers: true");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
*/
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
res = curl_easy_perform(curl);
if (res != CURLE_OK) { config_error("curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); }
else { config_status("Received data: %*\n", chunk.memory); }
// response log file (by config_status() func)
// Received data: {
free(chunk.memory);
curl_easy_cleanup(curl);
}
return 0;
}
[ad_2]