Implementing libcurl: Difference between revisions
Jump to navigation
Jump to search
Note:Remember that you will need to repeat steps 8-10 in both Release and Debug configurations.
TomEdwards (talk | contribs) m (→Windows) |
TomEdwards (talk | contribs) (→Usage: print web page to console) |
||
Line 29: | Line 29: | ||
* [http://curl.haxx.se/libcurl/c/ The libcurl C API] | * [http://curl.haxx.se/libcurl/c/ The libcurl C API] | ||
* [http://curl.haxx.se/libcurl/c/example.html Some examples] | * [http://curl.haxx.se/libcurl/c/example.html Some examples] | ||
Here is how to write the contents of a web page to the console: | |||
<source lang=cpp> | |||
// Called when curl receives data from the server | |||
size_t rcvData( void *ptr, size_t size, size_t nmemb, void *userdata) | |||
{ | |||
char data = char(ptr); | |||
char buffer[CURL_MAX_WRITE_SIZE]; | |||
strncat(buffer,&data,size); | |||
Msg(buffer); | |||
return size; | |||
} | |||
void PrintPage() | |||
{ | |||
curl = curl_easy_init(); | |||
curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se"); | |||
curl_easy_perform(curl); | |||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rcvData); | |||
curl_easy_cleanup(curl); | |||
} | |||
ConCommand print_page("print_page", PrintPage); | |||
</source> | |||
[[Category:Programming]] | [[Category:Programming]] | ||
[[Category:Free source code]] | [[Category:Free source code]] |
Revision as of 07:28, 6 September 2010
libcurl is a free library that can download data from the internet. It supports just about every protocol imaginable, and is available under a MIT/X derivative license.
Implementation
Windows
- Download the latest version of libcurl.
- Extract the
\lib
and\include
folders to a convenient location. - Open libcurl's VS project and change it to Release mode.
- Go to libcurl > Properties > C/C++ > Preprocessor > Definitions and add
CURL_STATICLIB
. Unless you really do want to use LDAP, andHTTP_ONLY
too. - Go to libcurl > Properties > C/C++ > Code Generation > Runtime Library and change it to read
Multi-threaded (/MT)
. - Build libcurl.
- Add
libcurl.lib
to your main project. The easiest way is to drag it onto the Solution Explorer. - Go to Your Project > Properties > C/C++ > Preprocessor > Definitions and add
CURL_STATICLIB
, as you did to curl itself. - Go to Your Project > Properties > C/C++ > General > Additional Include Directories and add the libcurl
\include
folder you extracted earlier. - Go to Your Project > Linker > Input > Additional Dependencies and add
ws2_32.lib
. If you want to use LDAP, addwldap32.lib
as well. #include "curl/curl.h"
and start coding!curl/easy.h
is helpful too.

Linux
[Todo]
Usage
Here is how to write the contents of a web page to the console:
// Called when curl receives data from the server
size_t rcvData( void *ptr, size_t size, size_t nmemb, void *userdata)
{
char data = char(ptr);
char buffer[CURL_MAX_WRITE_SIZE];
strncat(buffer,&data,size);
Msg(buffer);
return size;
}
void PrintPage()
{
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
curl_easy_perform(curl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rcvData);
curl_easy_cleanup(curl);
}
ConCommand print_page("print_page", PrintPage);