coding libcurl in C
download: http://curl.haxx.se/download.html
latest version: latest:curl-7.21.2
libcurl: http://curl.haxx.se/libcurl/
all about libcurl with C: http://curl.haxx.se/libcurl/c/
You must include:
<curl/curl.h>
compile
gcc libcurl1.c -o libcurl1 -lcurl
important structures:
CURL *curl;
CURLcode curl_res;
CURLINFO info;
Start a libcurl easy session
curl = curl_easy_init();
see more info: http://curl.haxx.se/libcurl/c/curl_easy_init.html
set options for a curl easy handle
curl_easy_setopt(CURL *handle, CURLoption option, parameter);
CURLOPT_WRITEDATA Data pointer to pass to the file write function
curl_easy_setopt(curl, CURLOPT_WRITEDATA, tmp);
tmp: FILE handle
set proxy
curl_easy_setopt(curl, CURLOPT_PROXY, pxy);
Perform a file transfer
curl_easy_perform(CURL * handle );
End a libcurl easy session
curl_easy_cleanup(curl);
set an url
curl_easy_setopt(curl, CURLOPT_URL, url);
set file handle, where the data get writed
curl_easy_setopt(curl, CURLOPT_WRITEDATA, tmp);
download the page
curl_res = curl_easy_perform(curl);
get http status code
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
get size of downloaded page
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &c_length);
sample: libcurl1.c
##########################################
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <fcntl.h> #include <stdlib.h> #include <curl/curl.h> int main() { CURL *curl; CURLcode curl_res; CURLINFO info; long http_code; double c_length; FILE *tmp; tmp=fopen("/tmp/google_index.html", "w"); if(tmp==NULL) { printf("ERROR to open file /tmp/google_index.html\n"); exit(2); } printf("init curl session\n"); curl = curl_easy_init(); printf("set url to download\n"); curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.de/index.html"); printf("set file handler to write\n"); curl_easy_setopt(curl, CURLOPT_WRITEDATA, tmp); printf("download the file\n"); curl_res = curl_easy_perform(curl); if(curl_res==0) { printf("file downloaded\n"); } else { printf("ERROR in dowloading file\n"); fclose(tmp); curl_easy_cleanup(curl); } printf("get http return code\n"); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); printf("http code: %lu\n", http_code); printf("get size of download page\n"); curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &c_length); printf("length: %g\n", c_length); printf("END: close all files and sessions\n"); fclose(tmp); curl_easy_cleanup(curl); return 0; }
##########################################
./libcurl1
init curl session
set url to download
set file handler to write
download the file
file downloaded
get http return code
http code: 200
get size of download page
length: 6969
END: close all files and sessions
sireesh
August 13, 2013 at 7:53 PM
hi we r using curl for downloading remote file curl_easy_perform returns 0 but the file is partially downloaded.any idea what could be the issue?
string url = string (“sftp://”) + hostip + “/” + srcFile;
curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ftpfile);
curl_easy_setopt (curl, CURLOPT_USERPWD, userpwd.c_str ());
curl_easy_setopt (curl, CURLOPT_FTP_RESPONSE_TIMEOUT, 2700L);
is this due to some n/w issue?