As of revision 71818, we ported the first four Eiffel cURL examples from http://curl.haxx.se/lxr/source/docs/examples/. You can find them located at $EIFFEL_SRC\examples\cURL folder.
If ever you want to provide other example, feel free to post them here or contact me directly at larryl@eiffel.com .
Examples:
Simple
class
APPLICATION
create
make
feature -- Initialization
make is
-- Run application.
local
l_result: INTEGER
do
print ("Eiffel cURL simple example.%N")
curl_handle := curl_easy.init
if curl_handle /= default_pointer then
-- First we specify which URL we would like to download.
curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_url, "www.google.com")
-- After `perform' has been called, the remote HTML source is printed in the console.
l_result := curl_easy.perform (curl_handle)
-- Always cleanup
curl_easy.cleanup (curl_handle)
end
end
feature {NONE} -- Implementation
curl_easy: CURL_EASY_EXTERNALS is
-- cURL easy externals
once
create Result
end
curl_handle: POINTER;
-- cURL handle
This example will retrieve the HTML source from Google and then print the fetched html code to the console. This is the simplest way to download a source from a URL.
Result console output:
Http_post
class
APPLICATION
create
make
feature -- Initialization
make is
-- Run application.
local
l_result: INTEGER
do
print ("Eiffel cURL http post example.%N")
curl_handle := curl_easy.init
if curl_handle /= default_pointer then
-- First set the URL that is about to receive our POST. This URL can
-- just as well be a https:// URL if that is what should receive the
-- data.
curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_url, "http://postit.example.com/moo.cgi")
-- Now specify the POST data
curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_postfields, "name=daniel&project=curl")
-- Perform the request, `l_result' will get the return code
l_result := curl_easy.perform (curl_handle)
-- Always cleanup
curl_easy.cleanup (curl_handle)
end
end
feature {NONE} -- Implementation
curl_easy: CURL_EASY_EXTERNALS is
-- cURL easy externals
once
create Result
end
curl_handle: POINTER;
-- cURL handle
This example posts something to target recipient page. The key is specify the URL to post to and any post name/value pairs.
Get_in_memory
class
APPLICATION
create
make
feature -- Initialization
make is
-- Run application.
local
l_result: INTEGER
l_curl_string: CURL_STRING
do
print ("Eiffel cURL get in memory example.%N")
create l_curl_string.make_empty
curl.global_init
-- Init the curl session
curl_handle := curl_easy.init
-- Specify URL to get
curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_url, "http://www.google.com")
-- Send all data to default Eiffel curl write function
curl_easy.set_write_function (curl_handle)
-- We pass our `l_curl_string''s object id to the callback function */
curl_easy.setopt_integer (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_writedata, l_curl_string.object_id)
-- Get it!
l_result := curl_easy.perform (curl_handle)
-- Cleanup curl stuff
curl_easy.cleanup (curl_handle)
--Now, our `l_curl_string' contains the remote html source codes.
--Do something nice with it!
if not l_curl_string.is_empty then
print ("Remote html source got. Size is: " + l_curl_string.count.out + ". ")
end
--You don't need to be aware of memory management issue since Eiffel will handle all of them for you.
curl.global_cleanup
end
feature {NONE} -- Implementation
curl: CURL_EXTERNALS is
-- cURL externals
once
create Result
end
curl_easy: CURL_EASY_EXTERNALS is
-- cURL easy externals
once
create Result
end
curl_string: CURL_STRING
-- String used by Eiffel cURL library.
curl_handle: POINTER;
-- cURL handle
This example will retrieve the HTML source from Google and then write it into an Eiffel STRING object. This example will download the HTML source to Eiffel string object only.
The key is first we specify the cURL "write data" with l_curl_string's object id. Then we can continue to download the HTML source into
l_curl_string'.
Result console output:
Debug
class
APPLICATION
create
make
feature -- Initialization
make is
-- Run application.
local
l_result: INTEGER
do
print ("Eiffel cURL debug example.%N")
curl_handle := curl_easy.init
if curl_handle /= default_pointer then
curl_easy.set_debug_function (curl_handle)
-- The DEBUGFUNCTION has no effect until we enable VERBOSE
curl_easy.setopt_integer (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_verbose, 1)
curl_easy.setopt_string (curl_handle, {CURL_OPT_CONSTANTS}.curlopt_url, "www.google.com")
l_result := curl_easy.perform (curl_handle)
-- Always cleanup
curl_easy.cleanup (curl_handle)
end
end
feature {NONE} -- Implementation
curl_easy: CURL_EASY_EXTERNALS is
-- cURL easy externals
once
create Result
end
curl_handle: POINTER;
-- cURL handle
This example shows how to use the debug facilities of cURL.
The key is once we enabled cURL debug option, we can found all kinds of detailed send and receive information in console output.
Result console output:
Follow-up to this example ...
I found out from Manu Stapf that the cURL library is what I can use to access various forms of web site data using Eiffel. This works really great. However, I did have several hurdles to overcome along the way.
First: You may find that you are missing several *.LIB files in the "..\cURL\spec\msc\windows\lib" (e.g. eiffel_curl.lib, ileiffel_curl.lib and mteiffel_curl.lib). If so, then you will need to run a "finish_freezing -library" command at a command prompt. There are a couple of points about that as well. First, you need to find the Clib directory directly under the cURL directory as it contains a number of *.SH files and so on required for finish_freezing.exe. CD to this directory and run "finish_freezing -library". If your system complains that it doesn't recognize this command (e.g. finish_freezing) then you will need to make a direct reference to it. I had to do it this way: ..\cURL\Clib>"C:\Program Files (x86)\Eiffel Software\EiffelStudio 6.7 Enterprise\studio\spec\windows\bin\finish_freezing.exe" -library
Second: Once you have the appropriate LIB files built with finish_freezing, then you may get a precondition require contract failure in: {CURL_EXTERNALS}.global_init ... dynamic_library_exists: is_dynamic_library_exists. If you do, then you need to ensure some DLL files are sitting next to either your W_Code EXE or your F_Code EXE. Specifically: The DLLs are located in the EiffelStudio delivery in studio/spec/windows/bin and the DLLs are: libcurl.dll, libeay32.dll and ssleay32.dll
With these two bits in place, then the software examples above ought to run without issue and as advertised.