Implementing libcurl: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (mmmm saving it)
(Rewrite Template:Lang to Template:LanguageBar. This action was performed by a bot.)
 
(20 intermediate revisions by 5 users not shown)
Line 1: Line 1:
== What is libcurl? ==
{{Underlinked|date=January 2024}}
{{LanguageBar|Implementing libcurl}}
{{toc-right}}


[http://curl.haxx.se/libcurl/ libcurl] is a free and easy-to-use client-side URL transfer library that allows you to transfer files across the internet through various protocols such as HTTP. It is the base of [http://curl.haxx.se/ cURL] a very popular command line tool for transferring data.
'''[http://curl.haxx.se/libcurl/ libcurl]''' is a free library that can download data from the internet. It supports just about every protocol imaginable, and is available [http://curl.haxx.se/docs/copyright.html under a MIT/X derivative license].


A few examples of the things that you could do by implementing libcurl into your source engine mod would be to set up an in game poll, download game content, upload player data. The possibilities are endless.
== Implementation ==


== The Installation ==
=== Windows ===


You will need to download the latest version of cURL. You can do this by visiting the [http://curl.haxx.se/latest.cgi?curl=zip Download page] on their website.
# [http://curl.haxx.se/latest.cgi?curl=zip Download the latest version of libcurl.]
# Extract the <code>\lib</code> and <code>\include</code> 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 <code title="The official docs say -DCURL_STATICLIB...they are wrong!">CURL_STATICLIB</code>. Unless you really do want to use [[wikipedia:LDAP|LDAP]], add <code>HTTP_ONLY</code> too.
# Go to ''libcurl > Properties > C/C++ > Code Generation > Runtime Library'' and change it to read <code>Multi-threaded (/MT)</code>.
# '''Build libcurl.'''
# Add <code>libcurl.lib</code> 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 <code title="The official docs say -DCURL_STATICLIB...they are wrong!">CURL_STATICLIB</code>, as you did to curl itself.
# Go to ''Your Project > Properties > C/C++ > General > Additional Include Directories'' and add the libcurl <code>\include</code> folder you extracted earlier.
# Go to ''Your Project > Linker > Input > Additional Dependencies'' and add <code>ws2_32.lib</code>. If you want to use LDAP, add <code>wldap32.lib</code> as well.
# <code>#include "curl/curl.h"</code> and start coding!


Proceed to extract the zip and open your mod folder, and your mods solution in VS.
{{note|Remember that you will need to repeat steps 8-10 in both Release and Debug configurations.}}


In your modname\src\ folder create a new folder named libcurl.
=== Linux ===


Open the curl folder that you just extracted, browse to curl-#.##.#\lib and copy all of the files in there.
{{todo}}


Paste them into your new modname\src\libcurl folder.
== Usage ==


Back in the your recently extracted curl folder browse to curl-#.##.#\include and copy the curl folder into your modname\src\libcurl folder.
* [http://curl.haxx.se/libcurl/c/ The libcurl C API]
* [http://curl.haxx.se/libcurl/c/example.html Some examples]


In VS right click your solution, select Add -> Existing Project. Select modname\src\libcurl\libcurl.vcproj.
Here is how to write the contents of a web page to the console:


VS2008 and 2010 only: Run through the conversion wizard.
<source lang=cpp>
#include "curl/curl.h"


Open the properties for your libcurl project.
// Called when curl receives data from the server
static size_t rcvData(void *ptr, size_t size, size_t nmemb, void *userdata)
{
Msg((char*)ptr); // up to 989 characters each time
return size * nmemb;
}


void PrintPage()
{
        CURL *curl;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "developer.valvesoftware.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rcvData);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}


ConCommand print_page("print_page", PrintPage);
</source>


 
[[Category:Programming]]
 
[[Category:Free source code]]
==  The curl license ==
libcurl is licensed under a MIT/X derivative license
 
COPYRIGHT AND PERMISSION NOTICE
Copyright (c) 1996 - 2010, Daniel Stenberg, <daniel@haxx.se>.
All rights reserved.
Permission to use, copy, modify, and distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright
notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

Latest revision as of 17:25, 18 July 2025

Underlinked - Logo.png
This article needs more Wikipedia icon links to other articles to help Wikipedia icon integrate it into the encyclopedia. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024
English (en)Deutsch (de)Translate (Translate)

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

  1. Download the latest version of libcurl.
  2. Extract the \lib and \include folders to a convenient location.
  3. Open libcurl's VS project and change it to Release mode.
  4. Go to libcurl > Properties > C/C++ > Preprocessor > Definitions and add CURL_STATICLIB. Unless you really do want to use LDAP, add HTTP_ONLY too.
  5. Go to libcurl > Properties > C/C++ > Code Generation > Runtime Library and change it to read Multi-threaded (/MT).
  6. Build libcurl.
  7. Add libcurl.lib to your main project. The easiest way is to drag it onto the Solution Explorer.
  8. Go to Your Project > Properties > C/C++ > Preprocessor > Definitions and add CURL_STATICLIB, as you did to curl itself.
  9. Go to Your Project > Properties > C/C++ > General > Additional Include Directories and add the libcurl \include folder you extracted earlier.
  10. Go to Your Project > Linker > Input > Additional Dependencies and add ws2_32.lib. If you want to use LDAP, add wldap32.lib as well.
  11. #include "curl/curl.h" and start coding!
Note.pngNote:Remember that you will need to repeat steps 8-10 in both Release and Debug configurations.

Linux

[Todo]

Usage

Here is how to write the contents of a web page to the console:

#include "curl/curl.h"

// Called when curl receives data from the server
static size_t rcvData(void *ptr, size_t size, size_t nmemb, void *userdata)
{
	Msg((char*)ptr); // up to 989 characters each time
	return size * nmemb;
}

void PrintPage()
{
        CURL *curl;
	curl = curl_easy_init();
	curl_easy_setopt(curl, CURLOPT_URL, "developer.valvesoftware.com");
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, rcvData);
	curl_easy_perform(curl);
	curl_easy_cleanup(curl);
}

ConCommand print_page("print_page", PrintPage);