PHP utility collection with hybrid and fluent APIs.

⌈⌋ branch:  hybrid7 libraries


curl() wraps curl{}

curl.php provides a prefix-free and fluent wrapper onto curl functions.


curl() is a hybrid instantiation function. It transforms its input into a curl{} object.
It can be construed with:

  • No arguments.

  • A single URL.

  • An array of key=>value settings.

       curl(array("URL" => $url, "POST" => 1))
  • From a plain curl resource handle.

       curl( curl_init($url) )
  • Or rewrap an existing curl{} object.

       curl( new curl() )

All curl constants become available as virtual methods/properties; just without CURLOPT_ prefix, and case-insensitively.
It's just a new notational style, but identical identifiers:

curl_setopt($ch, CURLOPT_REFERER, ...) $curl->referer(...)
curl_setopt($ch, CURLOPT_COOKIE, ...) $curl->COOKIE(...)
curl_setopt($ch, CURLOPT_HTTPHEADER, ...) $curl->HttpHeader(...)
curl_setopt($ch, CURLOPT_TIMEOUT, ...) $curl->timeout(...)
curl_setopt($ch, CURLOPT_COOKIEJAR, ...) $curl->COOKIEJAR(...)
curl_setopt($ch, CURLOPT_PORT, ...) $curl->port(...)
curl_setopt($ch, CURLOPT_CRLF, ...) $curl->CRLF(...)

You could create a curl{} object thus also with:

  $curl = curl()->URL($url);

For setting an option use:

  $curl->Post($value);     // can be chained

Use a virtual property name for retrieving an option or result state:

  $value = $curl->filetime;

All curl_*() functions become available as methods as well:


Calling such methods obviously terminates the fluent chain.
->exec() for instance promptly returns the result content.


You can also wrap multiple curl handles or objects in a multi-request object. It can be constructed from single URLs or curl objects or raw resource handles again.

While it accepts a plain list of arguments, it's advisable to pass a named set:

  $cm = curl_multi(array(
     "google" => "",
     "url2" => $url2,
     "pers" => curl($url3),
     "mixed" => array( "URL" => $url4, "POST" => 1 ),

Then options can be applied across all bound curl{} objects:


And when you ->exec() the chain, it will return all result bodies at once:

  $array = $cm->exec();

Which would contain ["google"] and ["url2"] for instance.

Btw, ->exec() accepts a float as parameter. It defines a timeout until which all chained curl requests must have finished. Else some of the result bodies will be empty.

For an example on the flexible instantiation and use see social.count.php