help files
Z505 | PasWiki | FUQ | Search | Main Docs | API Guide



Notes

When to use cURL?

cURL is useful for when the http.pas and hostname.pas units from the Powtils don't offer what you need. cURL can be used for example to grab SSL/HTTPS pages which at current time of writing the http.pas unit cannot do.

cURL also offers access to FTP and FTPS which is again something the http.pas unit cannot do. cURL is very well known and installed on almost all systems and servers.

Our friend Jeff (YetAnotherGeek) maintains the CurlPas wrapper that makes cURL easy to access through Pascal programs. (Thanks Jeff!)

Download cURL interface

The urls to the project and downloads are:

Other similar packages

There is also the Synapse project (by Lukas) and the LNet project (by Almindor) to check out. cURL is more geared toward grabbing web content without having to use low level sockets code. lNet and Synapse are more geared for mid or lower level sockets programming; however they still overlap some features.

Ugly cURL Example

program cURLtest; {$mode objfpc} {$H+}

uses
  pwumain,  // web functions
  {$IFDEF UNIX}libc,{$ENDIF}
  curl_h; // Jeff's (yetanothergeek) Curl C API wrapper

var 
  hCurl: pCurl;

function writer(p: pchar; sz: size_t; nmemb: size_t; stream: pointer): size_t; cdecl;
var
  s: string;
  len: longword;
  tmpp: pchar;
begin
  len:= sz * nmemb;
  if len <> 0 then
  begin
    getmem(tmpp, len + 1); // allocate memory for TEMPORARY buffer
    move(p^, tmpp^, len); // copies data sent to us into a TEMPORARY buffer
    tmpp<len>:= #0; // note: actually square brackets, lufdoc needs to be updated
    // DEAL WITH DATA HERE, INSTEAD OF WEBWRITE YOU COULD USE A STRING OR BUFFER
    webwrite('<p>------------------ BEGIN DATA CHUNK -------------------------<p>');
    webwrite(string(tmpp));  
    webwrite('<p>------------------- END DATA CHUNK --------------------------<p>');
    freemem(tmpp);
  end;
  result:= len;
end;

//var
//  p: pointer;

begin
  hCurl:= curl_easy_init;
  if ( hCurl <> nil ) then 
  begin
    curl_easy_setopt(hCurl, CURLOPT_VERBOSE, true);
    curl_easy_setopt(hCurl, CURLOPT_URL, 'curl.haxx.se');
    curl_easy_setopt(hCurl, CURLOPT_FOLLOWLOCATION ,true);
    //curl_easy_setopt(hCurl,CURLOPT_WRITEDATA, p);
    curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, @writer);
    curl_easy_perform(hCurl);
    curl_easy_cleanup(hCurl);
  end;
end.
The above example uses the simple libcURL C API however you can use Jeff's curl objects or other curl wrappers (there are a few out there).

The writer() function above is what deals with the data.. you could store the data in a string and then parse it based on your needs, or do whatever with it. In the above simple example I simply write the data to web browser output as soon as each chunk is received.

Also, the default cURL naming convention is kind of ugly and verbose (i.e. size_t and curl_easy_setopt ) - you could wrap that into a nicer naming scheme or you can use existing Curl objects from Jeff which have a nicer naming scheme. At some point the Powerful Web Utils package may contain a simple Curl wrapper unit with a nice naming convention that matches the Powerful Web Utils naming convention (CurlGet, CurlSetOpt, etc).






lufdoc, Powtils, fpc, freepascal, delphi, kylix, c/c++, mysql, cgi web framework docs, Z505