1libcurl(3) libcurl overview libcurl(3)
2
3
4
6 libcurl - client-side URL transfers
7
9 This is a short overview on how to use libcurl in your C programs.
10 There are specific man pages for each function mentioned in here. There
11 are also the libcurl-easy(3) man page, the libcurl-multi(3) man page,
12 the libcurl-share(3) man page and the libcurl-tutorial(3) man page for
13 in-depth understanding on how to program with libcurl.
14
15 There are many bindings available that bring libcurl access to your
16 favourite language. Look elsewhere for documentation on those.
17
18 libcurl has a global constant environment that you must set up and
19 maintain while using libcurl. This essentially means you call
20 curl_global_init(3) at the start of your program and
21 curl_global_cleanup(3) at the end. See GLOBAL CONSTANTS below for
22 details.
23
24 If libcurl was compiled with support for multiple SSL backends, the
25 function curl_global_sslset(3) can be called before curl_global_init(3)
26 to select the active SSL backend.
27
28 To transfer files, you create an "easy handle" using curl_easy_init(3)
29 for a single individual transfer (in either direction). You then set
30 your desired set of options in that handle with curl_easy_setopt(3).
31 Options you set with curl_easy_setopt(3) stick. They will be used on
32 every repeated use of this handle until you either change the option,
33 or you reset them all with curl_easy_reset(3).
34
35 To actually transfer data you have the option of using the "easy"
36 interface, or the "multi" interface.
37
38 The easy interface is a synchronous interface with which you call
39 curl_easy_perform(3) and let it perform the transfer. When it is com‐
40 pleted, the function returns and you can continue. More details are
41 found in the libcurl-easy(3) man page.
42
43 The multi interface on the other hand is an asynchronous interface,
44 that you call and that performs only a little piece of the transfer on
45 each invoke. It is perfect if you want to do things while the transfer
46 is in progress, or similar. The multi interface allows you to select()
47 on libcurl action, and even to easily download multiple files simulta‐
48 neously using a single thread. See further details in the libcurl-
49 multi(3) man page.
50
51 You can have multiple easy handles share certain data, even if they are
52 used in different threads. This magic is setup using the share inter‐
53 face, as described in the libcurl-share(3) man page.
54
55 There is also a series of other helpful functions to use, including
56 these:
57
58 curl_version_info()
59 gets detailed libcurl (and other used libraries) version
60 info
61
62 curl_getdate()
63 converts a date string to time_t
64
65 curl_easy_getinfo()
66 get information about a performed transfer
67
68 curl_formadd()
69 helps building an HTTP form POST
70
71 curl_formfree()
72 free a list built with curl_formadd(3)
73
74 curl_slist_append()
75 builds a linked list
76
77 curl_slist_free_all()
78 frees a whole curl_slist
79
80
82 On unix-like machines, there's a tool named curl-config that gets
83 installed with the rest of the curl stuff when 'make install' is per‐
84 formed.
85
86 curl-config is added to make it easier for applications to link with
87 libcurl and developers to learn about libcurl and how to use it.
88
89 Run 'curl-config --libs' to get the (additional) linker options you
90 need to link with the particular version of libcurl you've installed.
91 See the curl-config(1) man page for further details.
92
93 Unix-like operating system that ship libcurl as part of their distribu‐
94 tions often don't provide the curl-config tool, but simply install the
95 library and headers in the common path for this purpose.
96
97 Many Linux and similar systems use pkg-config to provide build and link
98 options about libraries and libcurl supports that as well.
99
101 All public functions in the libcurl interface are prefixed with 'curl_'
102 (with a lowercase c). You can find other functions in the library
103 source code, but other prefixes indicate that the functions are private
104 and may change without further notice in the next release.
105
106 Only use documented functions and functionality!
107
109 libcurl works exactly the same, on any of the platforms it compiles and
110 builds on.
111
113 libcurl is thread safe but there are a few exceptions. Refer to
114 libcurl-thread(3) for more information.
115
116
118 Persistent connections means that libcurl can re-use the same connec‐
119 tion for several transfers, if the conditions are right.
120
121 libcurl will always attempt to use persistent connections. Whenever you
122 use curl_easy_perform(3) or curl_multi_perform(3) etc, libcurl will
123 attempt to use an existing connection to do the transfer, and if none
124 exists it'll open a new one that will be subject for re-use on a possi‐
125 ble following call to curl_easy_perform(3) or curl_multi_perform(3).
126
127 To allow libcurl to take full advantage of persistent connections, you
128 should do as many of your file transfers as possible using the same
129 handle.
130
131 If you use the easy interface, and you call curl_easy_cleanup(3), all
132 the possibly open connections held by libcurl will be closed and for‐
133 gotten.
134
135 When you've created a multi handle and are using the multi interface,
136 the connection pool is instead kept in the multi handle so closing and
137 creating new easy handles to do transfers will not affect them. Instead
138 all added easy handles can take advantage of the single shared pool.
139
141 There are a variety of constants that libcurl uses, mainly through its
142 internal use of other libraries, which are too complicated for the
143 library loader to set up. Therefore, a program must call a library
144 function after the program is loaded and running to finish setting up
145 the library code. For example, when libcurl is built for SSL capabil‐
146 ity via the GNU TLS library, there is an elaborate tree inside that
147 library that describes the SSL protocol.
148
149 curl_global_init(3) is the function that you must call. This may allo‐
150 cate resources (e.g. the memory for the GNU TLS tree mentioned above),
151 so the companion function curl_global_cleanup(3) releases them.
152
153 The basic rule for constructing a program that uses libcurl is this:
154 Call curl_global_init(3), with a CURL_GLOBAL_ALL argument, immediately
155 after the program starts, while it is still only one thread and before
156 it uses libcurl at all. Call curl_global_cleanup(3) immediately before
157 the program exits, when the program is again only one thread and after
158 its last use of libcurl.
159
160 You can call both of these multiple times, as long as all calls meet
161 these requirements and the number of calls to each is the same.
162
163 It isn't actually required that the functions be called at the begin‐
164 ning and end of the program -- that's just usually the easiest way to
165 do it. It is required that the functions be called when no other
166 thread in the program is running.
167
168 These global constant functions are not thread safe, so you must not
169 call them when any other thread in the program is running. It isn't
170 good enough that no other thread is using libcurl at the time, because
171 these functions internally call similar functions of other libraries,
172 and those functions are similarly thread-unsafe. You can't generally
173 know what these libraries are, or whether other threads are using them.
174
175 The global constant situation merits special consideration when the
176 code you are writing to use libcurl is not the main program, but rather
177 a modular piece of a program, e.g. another library. As a module, your
178 code doesn't know about other parts of the program -- it doesn't know
179 whether they use libcurl or not. And its code doesn't necessarily run
180 at the start and end of the whole program.
181
182 A module like this must have global constant functions of its own, just
183 like curl_global_init(3) and curl_global_cleanup(3). The module thus
184 has control at the beginning and end of the program and has a place to
185 call the libcurl functions. Note that if multiple modules in the pro‐
186 gram use libcurl, they all will separately call the libcurl functions,
187 and that's OK because only the first curl_global_init(3) and the last
188 curl_global_cleanup(3) in a program change anything. (libcurl uses a
189 reference count in static memory).
190
191 In a C++ module, it is common to deal with the global constant situa‐
192 tion by defining a special class that represents the global constant
193 environment of the module. A program always has exactly one object of
194 the class, in static storage. That way, the program automatically
195 calls the constructor of the object as the program starts up and the
196 destructor as it terminates. As the author of this libcurl-using mod‐
197 ule, you can make the constructor call curl_global_init(3) and the
198 destructor call curl_global_cleanup(3) and satisfy libcurl's require‐
199 ments without your user having to think about it. (Caveat: If you are
200 initializing libcurl from a Windows DLL you should not initialize it
201 from DllMain or a static initializer because Windows holds the loader
202 lock during that time and it could cause a deadlock.)
203
204 curl_global_init(3) has an argument that tells what particular parts of
205 the global constant environment to set up. In order to successfully
206 use any value except CURL_GLOBAL_ALL (which says to set up the whole
207 thing), you must have specific knowledge of internal workings of
208 libcurl and all other parts of the program of which it is part.
209
210 A special part of the global constant environment is the identity of
211 the memory allocator. curl_global_init(3) selects the system default
212 memory allocator, but you can use curl_global_init_mem(3) to supply one
213 of your own. However, there is no way to use curl_global_init_mem(3)
214 in a modular program -- all modules in the program that might use
215 libcurl would have to agree on one allocator.
216
217 There is a failsafe in libcurl that makes it usable in simple situa‐
218 tions without you having to worry about the global constant environment
219 at all: curl_easy_init(3) sets up the environment itself if it hasn't
220 been done yet. The resources it acquires to do so get released by the
221 operating system automatically when the program exits.
222
223 This failsafe feature exists mainly for backward compatibility because
224 there was a time when the global functions didn't exist. Because it is
225 sufficient only in the simplest of programs, it is not recommended for
226 any program to rely on it.
227
228
229
230libcurl 7.61.1 July 15, 2017 libcurl(3)