Curly is an object oriented wrapper around PHP's cURL extension.
To execute requests you may provide a URL and HTTP method, but you may also provide an instance of PSR-7's
RequestInterface as a base to configure the cURL channel.
<?php $curly = new Curly(); // Using PSR-7 RequestInterface implementation $request = (new Request) ->withMethod(Curly::HTTP_POST) ->withUri(new Uri('http://example.com')) ->withHeader('Accepts', 'application/json'); $response = $curly->request($request); // Using plain URL and method $response = $curly->requestByUrl('http://example.com', Curly::HTTP_DELETE);
By default the
request() methods will return a
ResponseInterface. To process the response
manually, you may configure callbacks or configure a custom handler (which under water will configure callbacks, but
provides a cleaner programming interface).
The options that would normally be set through
curl_setopt must be set through a
CurlyOptions instance. The
CurlyOptions instance can be reused over multiple requests.
Instead of manually configuring callbacks in the
CurlyOptions object, you may extend the
AbstractHandler class to
hook into events. The library comes with two implementations of the
CancellableHandler is provided a callback during instantiation. During the transfer, cURL's progress event is
routed to the handler, which in turn invokes the callback to determine whether the transfer should be aborted.
StreamHandler decorates the
CancellableHandler and is an example of a handler that hooks into cURL's read and
write callbacks. It probably isn't very useful since setting the
outputStream options in the
CurlyOptions object would achieve more or less the same, but helps demonstrate the usage of the callbacks.