About
Following the example in the libcurl Tutorial, let's make a simple demonstration program to use the basic cURL facilities.Usage
curltut01 URLThe specified URL is downloaded and echoed on standard output, along with other information for demonstration purposes. If an error occurs, the error message from Curl is displayed.
Code Listing
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
char errorbuffer[CURL_ERROR_SIZE] = "(UNKNOWN ERROR)";
void error(){printf("An error occured with a curl function: %s\n", errorbuffer);}
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
typedef struct {
char *filename;
FILE *fp;
} UserData;
#define SELF_EXE argv[0]
#define URL argv[1]
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("Usage: %s URL\n", SELF_EXE);
exit(1);
}
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
error();
goto aft_curl_global_cleanup;
}
CURL *easyhandle = curl_easy_init();
if (easyhandle == NULL) {
error();
goto aft_curl_easy_cleanup;
}
if (curl_easy_setopt(easyhandle, CURLOPT_ERRORBUFFER, errorbuffer) != CURLE_OK) {
error();
goto done;
}
if (curl_easy_setopt(easyhandle, CURLOPT_VERBOSE, 1) != CURLE_OK) {
error();
goto done;
}
if (curl_easy_setopt(easyhandle, CURLOPT_HEADER, 1) != CURLE_OK) {
error();
goto done;
}
if (curl_easy_setopt(easyhandle, CURLOPT_STDERR, stderr) != CURLE_OK) {
error();
goto done;
}
if (curl_easy_setopt(easyhandle, CURLOPT_URL, URL) != CURLE_OK) {
error();
goto done;
}
if (curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, write_data) != CURLE_OK) {
error();
goto done;
}
UserData userdata = {.filename = "somefilename", .fp=stdout};
if (curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &userdata) != CURLE_OK) {
error();
goto done;
}
if (curl_easy_perform(easyhandle) != CURLE_OK) {
error();
goto done;
}
done:
curl_easy_cleanup(easyhandle);
aft_curl_easy_cleanup:
curl_global_cleanup();
aft_curl_global_cleanup:
return 0;
}
#define BAR "\
-------------------------------------------------------------------------------"
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
UserData *ud = userp;
printf(BAR "\n");
printf("ud.filename: %s\n", ud->filename);
printf("buffer: ");
if (fwrite(buffer, size, nmemb, ud->fp) == nmemb) {
printf("\n" BAR "\n");
return size*nmemb; // success
}
printf("\n" BAR "\n");
return 0; // error
}
Keine Kommentare:
Kommentar veröffentlichen