Hi,
--- On Wed, Nov 16, 2011 at 10:16 AM, Girish Venkatachalam
<girishvenkatachalam at gmail.com> wrote:
| I asked you to send your code.
\--
libcurl is quite mature, and I am giving an example to show separation
of functions. It can also be used with https URLs. Make sure you have
libcurl, libcurl-devel installed on your system. Compile using:
$ gcc test.c -o test -lcurl
Test with:
$ ./test
=== BEGIN ===
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string.h>
size_t
write_data (void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written = 0;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int
curl_initialize (CURL **curl)
{
*curl = curl_easy_init();
return 0;
}
int
curl_download (CURL *curl, FILE *fp, char *url, char *outfilename)
{
CURLcode res = CURLE_OK;
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
}
else
printf ("curl is NULL!\n");
return 0;
}
void
curl_cleanup (CURL *curl, FILE *fp)
{
curl_easy_cleanup(curl);
if (fp)
fclose(fp);
}
int
main (int argc, char **argv)
{
CURL *curl = NULL;
FILE *fp = NULL;
char *url = "http://curl.haxx.se/libcurl/c/simple.html";
char outfilename[FILENAME_MAX] = "/tmp/result.html";
curl_initialize (&curl);
curl_download (curl, fp, url, outfilename);
curl_cleanup (curl, fp);
return 0;
}
=== END ===
There is scope for further abstraction, and refining of APIs - left as
an exercise.
SK
--
Shakthi Kannan
http://www.shakthimaan.com