Next Previous Contents

4. cURL Module Function Reference

4.1 curl_new

Synopsis

Instantiate a new Curl_Type object

Usage

Curl_Type curl_new(String_Type url)

Description

This function instantiates and returns a Curl_Type and returns it. It is a wrapper around the cURL library function curl_easy_init. The Curl_Type object is created with the CURLOPT_NOPROGRESS option set to 1, and CURLOPT_VERBOSE set to 0.

Upon failure, the function throws a CurlError exception.

See Also

curl_setopt, curl_multi_new, curl_perform

4.2 curl_setopt

Synopsis

Set an option for a specified Curl_Type object

Usage

curl_setopt (Curl_Type c, Int_Type opt, ...)

Description

The curl_setopt function is a wrapper around the cURL library function curl_easy_setopt. For more information about the options that this function takes, see curl_easy_setopt. Only the differences between the module function and the corresponding API function will be explained here.

The current version of the curl module does not support the CURLOPT_*DATA options. Instead, support for these options has been integrated into the corresponding callback functions. For example, the CURLOPT_WRITEDATA option has been merged with the CURLOPT_WRITEFUNCTION. Moreover the prototypes for the callbacks have been changed to simplify their use, as described below.

CURLOPT_WRITEFUNCTION

This option requires two parameters: a reference to the callback function, and a user-defined object to pass to that function. The callback function will be passed two arguments: the specified user-defined object, and a binary string to write. Upon failure, the function must return -1, and any other value will indicate success.

CURLOPT_READFUNCTION

This option requires two parameters: a reference to the callback function, and a user-defined object to pass to that function. The callback function will be passed two arguments: the specified user-defined object, and the maximum number of bytes to be read by the function. Upon success, the function should return a (binary) string, otherwise it should return NULL to indicate failure.

CURLOPT_WRITEHEADER

This option requires two parameters: a reference to the callback function, and a user-defined object to pass to that function. The callback function will be passed two arguments: the specified user-defined object, and a header string write. The function must return -1 upon failure, or any other integer to indicate success.

CURLOPT_PROGRESSFUNCTION

This option requires two parameters: a reference to the callback function, and a user-defined object to pass to that function. The callback function will be passed five arguments: the specified user-defined object, and four double precision floating point values that represent the total number of bytes to be downloaded, the total downloaded so far, the total to be uploaded, and the total currently uploaded. This function must return 0 to indicate success, or non-zero to indicate failure.

A number of the options in the cURL API take a linked list of strings. Instead of a linked list, the module requires an array of strings for such options, e.g.,

    curl_setopt (c, CURLOPT_HTTPHEADER, 
                 ["User-Agent: S-Lang curl module",
                  "Content-Type: text/xml; charset=UTF-8",
                  "Content-Length: 1234"]);

Example

The following example illustrates how to write the contents of a specified URL to a file, its download progress to stdout, and the contents of its header to variable:

    define write_callback (fp, str)
    {
       return fputs (str, fp);
    }
    define progress_callback (fp, dltotal, dlnow, ultotal, ulnow)
    {
       if (-1 == fprintf (fp, "Bytes Received: %d\n", int(dlnow)))
         return -1;
       if (dltotal > 0.0)
         {
            if (-1 == fprintf (fp, "Percent Received: %g\n",
                               dlnow/dltotal * 100.0))
              return -1;
         }
       return 0;
    }
    define header_callback (strp, str)
    {
       @strp += str;
       return 0;
    }
    define download_url (url, file)
    {
       variable fp = fopen (file, "w");
       variable c = curl_new (url);
       curl_setopt (c, CURLOPT_FOLLOWLOCATION);
       curl_setopt (c, CURLOPT_WRITEFUNCTION, &write_callback, fp);
       curl_setopt (c, CURLOPT_PROGRESSFUNCTION, &progress_callback, stdout);
       variable var = "";
       curl_setopt (c, CURLOPT_HEADERFUNCTION, &header_callback, &var);
       curl_perform (c);
       () = fprintf (stdout, "Header: %s\n", var);
    }

See Also

curl_new, curl_perform, curl_multi_new

4.3 curl_global_init

Synopsis

Initialize the Curl library

Usage

curl_global_init (flags)

Description

This function is a wrapper around the corresponding cURL library function. See its documentation for more information.

See Also

curl_global_cleanup

4.4 curl_global_cleanup

Synopsis

Finalize the Curl library

Usage

curl_global_cleanup

Description

This function is a wrapper around the corresponding cURL library function. See its documentation for more information.

See Also

curl_global_init

4.5 curl_perform

Synopsis

Transfer a file

Usage

curl_perform

Description

This function is a wrapper around the curl_easy_perform cURL library function. See its documentation for more information.

See Also

curl_new, curl_setopt, curl_multi_perform

4.6 curl_get_info

Synopsis

Get information about a Curl_Type object

Usage

info = curl_get_info (Curl_Type c, Int_Type type)

Description

This function returns information of the requested type from a Curl_Type object. The data returned depends upon the value of the type argument. For more information, see see the documentation for the cURL library curl_easy_getinfo function.

Example

This example shows how to use the curl_get_info function to obtain the effective URL used for the transfer.

    url = curl_get_info (c, CURLINFO_EFFECTIVE_URL);

See Also

curl_new, curl_multi_info_read

4.7 curl_close

Synopsis

Close a Curl_Type object

Usage

curl_close

Description

This function is a wrapper around the curl_easy_cleanup cURL library function. See its documentation for more information.

Notes

Normally there is no need to call this function because the module automatically frees any memory associated with the Curl_Type object when it is no longer referenced.

See Also

curl_new

4.8 curl_multi_new

Synopsis

Instantiate a new Curl_Multi_Type object

Usage

Curl_Multi_Type curl_multi_new ()

Description

This function is a wrapper around the curl_multi_init cURL library function. It creates a new instance of a Curl_Multi_Type object and returns it.

See Also

curl_multi_perform, curl_setopt

4.9 curl_multi_perform

Synopsis

Process a Curl_Multi_Type object

Usage

Int_Type curl_multi_perform (Curl_Multi_Type m [,Double_Type dt])

Description

This function is a wrapper around the curl_multi_perform cURL library function. However, the curl module function takes an additional argument (dt) that causes the function to wait up to that many seconds for one of the underlying Curl_Type objects to become ready for reading or writing. The function returns the number of Curl_Type.

See Also

curl_multi_new, curl_multi_length, curl_multi_add_handle

4.10 curl_multi_remove_handle

Synopsis

Remove a Curl_Type object from a Curl_Multi_Type

Usage

curl_multi_remove_handle (Curl_Multi_Type m, Curl_Type c)

Description

This function removes the specified Curl_Type object from the Curl_Multi_Type object. For more information, see the documentation for the corresponding cURL library function.

See Also

curl_multi_add_handle, curl_multi_new, curl_multi_perform

4.11 curl_multi_add_handle

Synopsis

Add a Curl_Type object to a Curl_Multi_Type

Usage

curl_multi_add_handle

Description

This function adds the specified Curl_Type object to the Curl_Multi_Type object. For more information, see the documentation for the corresponding cURL library function.

See Also

curl_multi_remove_handle, curl_multi_new, curl_multi_perform

4.12 curl_multi_close

Synopsis

Close a Curl_Multi_Type object

Usage

curl_multi_close (Curl_Multi_Type m)

Description

This function is a wrapper around the curl_multi_cleanup cURL library function. Any Curl_Multi_Type objects associated with the specified Curl_Multi_Type object will be removed from it.

See Also

curl_multi_new, curl_multi_remove_handle, curl_multi_info_read

4.13 curl_multi_info_read

Synopsis

Get information about a Curl_Multi_Type transfer

Usage

Curl_Type curl_multi_info_read (Curl_Multi_Type m [,Ref_Type info])

Description

This function retrieves information from the specified Curl_Multi_Type object about an individual completed transfer by one of its associated Curl_Type objects. If all of the associated Curl_Type objects are still being processed, the function will return NULL. Otherwise it returns the completed Curl_Type object. If an optional Ref_Type parameter is passed to the function, then upon return the the associated variable be set to an integer representing the completion status of the Curl_Type object. If the completion status is 0, then the transfer was successful, otherwise the individual transfer failed and the completion status gives the error code associated with the transfer. More infomation about the transfer may be obtained by calling the curl_get_info function.

Example

The curl_multi_info_read function should be called after a call to curl_multi_perform has indicated that a transfer has taken place. The following example repeatedly calls curl_multi_info_read until it returns NULL. Each time through the loop, the completed Curl_Type object is removed from the Curl_Multi_Type object.

        while (c = curl_multi_info_read (m, &status), c!=NULL)
          {
             curl_multi_remove_handle (m, c);
             url = curl_get_url (c);
             if (status == 0)
               vmessage ("Retrieved %s", url);
             else 
               vmessage ("Unable to retrieve %s", url);
          }

See Also

curl_multi_perform, curl_multi_remove_handle, curl_get_info

4.14 curl_get_url

Synopsis

Get the URL associated with a Curl_Type object

Usage

String_Type curl_get_url (Curl_Type c)

Description

This function returns the name of the URL that was used to instantiate the specified Curl_Type object.

See Also

curl_new, curl_get_info

4.15 curl_multi_length

Synopsis

Get the number of Curl_Type objects in a Curl_Multi_Type

Usage

Int_Type curl_multi_length (Curl_Multi_Type m)

Description

This function returns the number of Curl_Type objects contained in the specified Curl_Multi_Type object.

See Also

curl_multi_remove_handle, curl_multi_add_handle


Next Previous Contents