PHP utility collection with hybrid and fluent APIs.

⌈⌋ ⎇ branch:  hybrid7 libraries


Artifact [a98da6d3d2]

Artifact a98da6d3d22b46d04ac04bc770be6421cf673539:

Wiki page [curl] by mario 2014-03-22 02:38:03.
D 2014-03-22T02:38:03.416
L curl
N text/x-markdown
P 14e5a248e65850726a423b1c30cbb5f0cef4132a
U mario
W 3246
<h3> curl() wraps curl{} </h3>

<kbd>[curl.php](doc/trunk/php7/curl.php)</kbd> provides a prefix-free and fluent wrapper onto curl functions.

      curl($url)
         ->post(1)
         ->PostFields($data)
         ->exec();

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

   *  No arguments.  

           curl()

   *  A single URL.  

           curl($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](http://php.net/curl_setopt) become available as virtual methods/properties; just without `CURLOPT_`  prefix, and **case-insensitively**.  
It's just a new notational style, but identical identifiers:

<table width=80%>
<tr><th>Old</th><th>New</th></tr>
<tr><td>curl_setopt($ch, CURLOPT_REFERER, ...)</td> <td>$curl->referer(...)</td></tr>
<tr><td>curl_setopt($ch, CURLOPT_COOKIE, ...)</td> <td>$curl->COOKIE(...)</td></tr>
<tr><td>curl_setopt($ch, CURLOPT_HTTPHEADER, ...)</td> <td>$curl->HttpHeader(...)</td></tr>
<tr><td>curl_setopt($ch, CURLOPT_TIMEOUT, ...)</td> <td>$curl->timeout(...)</td></tr>
<tr><td>curl_setopt($ch, CURLOPT_COOKIEJAR, ...)</td> <td>$curl->COOKIEJAR(...)</td></tr>
<tr><td>curl_setopt($ch, CURLOPT_PORT, ...)</td> <td>$curl->port(...)</td></tr>
<tr><td>curl_setopt($ch, CURLOPT_CRLF, ...)</td> <td>$curl->CRLF(...)</td></tr>
</table>

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](http://php.net/curl_setopt):

      $value = $curl->filetime;

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

      $curl->close();

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


<h3> curl_multi() </h3>

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" => "http://google.com/",
         "url2" => $url2,
         "pers" => curl($url3),
         "mixed" => array( "URL" => $url4, "POST" => 1 ),
      ));

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

      $cm->ReturnTransfer(1);

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](http://fossil.include-once.org/php-scripts/artifact/0bf6cc919e3b108748b56072509083cda4b56b63)
Z 96b3a21193915d665fee9899867d0cb1