eiffelroom

blogEiffel cURL library examples

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:

curl_simple_screenshot

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:

cURL_get_in_memory_screenshot

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:

cURL_debug_screenshot

about - contact