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