curl-library
Re: Function to return the body of a page?
Date: Wed, 13 Mar 2002 14:02:10 -0700
Hello,
You said:
>
>You should look at getinmemory.c instead, That example does exactly
>what you describe.
>
Like this?
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
struct MemoryStruct {
char *memory;
size_t size;
};
size_t
WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
register int realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory) {
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
}
return realsize;
}
struct MemoryStruct get_data(char* URL, int* curl_handle)
{
struct MemoryStruct chunk;
chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
chunk.size = 0; /* no data at this point */
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, URL);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_FILE, (void *)&chunk);
/* get it! */
curl_easy_perform(curl_handle);
return chunk;
}
int main(int argc, char **argv)
{
/* initialize curl */
CURL *curl_handle;
/* init the curl session */
curl_handle = curl_easy_init();
process_data(get_data("http://127.0.0.1:50002"));
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return 0;
}
Received on 2002-03-13