libcurl examples

The native API for libcurl is in C so this chapter is focussed on examples
written in C. But since many language bindings for libcurl are thin, they
usually expose more or less the same functions and thus they can still be
interesting and educational for users of other languages, too.

Get a simple HTML page

This example just fetches the HTML and sends it to stdout:

  1. #include <stdio.h>
  2. #include <curl/curl.h>
  3. int main(void)
  4. {
  5. CURL *curl;
  6. CURLcode res;
  7. curl = curl_easy_init();
  8. if(curl) {
  9. curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
  10. /* Perform the request, res will get the return code */
  11. res = curl_easy_perform(curl);
  12. /* Check for errors */
  13. if(res != CURLE_OK)
  14. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  15. curl_easy_strerror(res));
  16. /* always cleanup */
  17. curl_easy_cleanup(curl);
  18. }
  19. return 0;
  20. }

Get a HTML page in memory

This example is a variation of the former that instead of sending the data to
stdout (which often is not what you want), this stores the received data in a
memory buffer that is made larger as the incoming data grows.

It accomplishes this by using the write callback to receive the data.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <curl/curl.h>
  5. struct MemoryStruct {
  6. char *memory;
  7. size_t size;
  8. };
  9. static size_t
  10. WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
  11. {
  12. size_t realsize = size * nmemb;
  13. struct MemoryStruct *mem = (struct MemoryStruct *)userp;
  14. mem->memory = realloc(mem->memory, mem->size + realsize + 1);
  15. if(mem->memory == NULL) {
  16. /* out of memory! */
  17. printf("not enough memory (realloc returned NULL)\n");
  18. return 0;
  19. }
  20. memcpy(&(mem->memory[mem->size]), contents, realsize);
  21. mem->size += realsize;
  22. mem->memory[mem->size] = 0;
  23. return realsize;
  24. }
  25. int main(void)
  26. {
  27. CURL *curl_handle;
  28. CURLcode res;
  29. struct MemoryStruct chunk;
  30. chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
  31. chunk.size = 0; /* no data at this point */
  32. curl_global_init(CURL_GLOBAL_ALL);
  33. /* init the curl session */
  34. curl_handle = curl_easy_init();
  35. /* specify URL to get */
  36. curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.example.com/");
  37. /* send all data to this function */
  38. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  39. /* we pass our 'chunk' struct to the callback function */
  40. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
  41. /* some servers don't like requests that are made without a user-agent
  42. field, so we provide one */
  43. curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
  44. /* get it! */
  45. res = curl_easy_perform(curl_handle);
  46. /* check for errors */
  47. if(res != CURLE_OK) {
  48. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  49. curl_easy_strerror(res));
  50. }
  51. else {
  52. /*
  53. * Now, our chunk.memory points to a memory block that is chunk.size
  54. * bytes big and contains the remote file.
  55. *
  56. * Do something nice with it!
  57. */
  58. printf("%lu bytes retrieved\n", (long)chunk.size);
  59. }
  60. /* cleanup curl stuff */
  61. curl_easy_cleanup(curl_handle);
  62. free(chunk.memory);
  63. /* we're done with libcurl, so clean it up */
  64. curl_global_cleanup();
  65. return 0;
  66. }

Submit a login form over HTTP

TBD

Get an FTP directory listing

TBD

Download an HTTPS page straight into memory

TBD

Upload data to an HTTP site without blocking

TBD