Download the PHP package p/iris without Composer
On this page you can find all versions of the php package p/iris. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package iris
Short Description Curl Wrapper to ease curl* and curl_multi* functions usage
License MIT
Informations about the package iris
THIS PACKAGE IS ABANDONNED PLEASE USE A MORE MODERN APPROACH LIKE guzzle
Iris
Iris
is a simple library that wrap cURL
functions to ease their use. Iris can dot that using a simple OOP approach to cURL
.
This package is compliant with PSR-1, PSR-2, and PSR-4.
Iris
was built to understand cURL
advance features. It is loosely based on phpmulticurl.
Features & Roadmap
- [TODO] Add Unit Testing
Requirements
You need PHP >= 5.3.0 and the cURL
extension to use Iris
but the latest stable version of PHP is recommended.
Installation
You may install the Iris
package with Composer.
Or you can download the library and point to its autoloading script.
Usage
- Sending a simple request
- Sending a batch of request
Documentation
Iris\Message
Instantiation
to perform simple requests you need to instantiate the P\Iris\Message
object.
Before anything else you may want to specify your own user agent. By defaut the library set its own user agent. You can override this behaviour by setting you own user agent using the P\Iris\Message::setUserAgent
method. Once set, the user agent won't change even if you reset the Iris\Message
object.
At any given time you can reset the current cURL
handler allowing you to perform multiple call using the same P\Iris\Message
object. Once resetted, the object will loose any reference to the old request, except for the user agent and the event listerners attached to it.
Adding Options
cURL
calls are all about options, P\Iris\Message
help you set them using the setOption
method.
the setOption
method can accept an array
as a single value or a cURL options and its value.
The options can be changed and altered at any given times before performing the request as their are applied only when the request is performed.
Performing a request
Using Procedural code
To perform a request you need to call the Iris\Message::execute
method.
You'll get access to:
- the response by using the
P\Iris\Message::getResponse
method. - the
cURL
request information using theP\Iris\Message::getInfo
method. This method by default return an array of all available information. If you are only interested in one value then use itsCURLINFO_*
index; - the
cURL
errors are accessible throughP\Iris\Message::getErrorCode
andP\Iris\Message::getErrorMessage
methods;
Using Events
Another way to deal with the result is by using event listener. The \Iris\Message
comes with a simple event listener mechanism. You can register as many callback functions as you want and they will be executed on request success or on request error like explain below:
Depending on the result of the cURL
request one of the event will be called.
Of course you can remove the callback by using the P\Iris\Message::removeListener
with the same parameters used to add the callback.
Simple Requests (GET, POST, PUT, DELETE, HEAD)
The class comes with specialized method to perform usual calls used in REST API:
P\Iris\Message::get
to issue aGET
request;P\Iris\Message::post
to issue aPOST
request;P\Iris\Message::put
to issue aPUT
request;P\Iris\Message::delete
to issue aDELETE
request;P\Iris\Message::head
to issue aHEAD
request;
These methods takes up to 3 parameters:
- the first parameter is the URL you want to request;
- the second parameter can be an array, a Traversable object or a string. This is the data that will be used with the URL;
- the third parameter is to indicate if the request must be delayed or not. By default, the request is not delayed and the value is
false
(ie: usefull when combine withP\Iris\Batch
);
Here's a simple example:
You do not need to use the P\Iris\Message::execute
method as it is called directly by the P\Iris\Message::get
method. If you do not want the request to be directly issue then you have to specify it using the third argument.
The request is registered but not execute, you will need to call explicitly P\Iris\Message::execute
or P\Iris\Batch::execute
to perform the request.
Iris\Envelope
This class is responsible for wrapping curl_multi_*
function. As it is you could use it to perform parallel calls using cURL
but really don't, there's a simpler way using P\Iris\Batch
.
The sole purpose of this function is to configure how cURL
may behave when using parallel request. You can specify:
-
the maximum number of parallel requests you want to perform. By default This number is
10
but you can change the number using thesetOption
method just like with theP\Iris\Message
class Of note:- Settings options was not possible before PHP 5.5
- The class comes with a constant called
P\Iris\Envelope::MAXCONNECTS
the value of this value matches the value of the PHP5.5+ constantCURLMOPT_MAXCONNECTS
so both constants are interchangeable.
- to set the
selectTimeout
and theexecTimeout
used bycURL
to perform the requests. These values can be set and get using simple getter and setter methods.- The
selectTimeout
must be a float; - The
execTimeout
must be a integer;
- The
Iris\Batch
Instantiation
This class was build to simplify parallel calls. To instantiate this class you must give it a object of type Iris\Enveloppe
Adding Request
Once the \P\Iris\Batch
is instantiate you just need to provide the cURL
requests to perform in parallel.
To do so you can use the \P\Iris\Batch::addOne
function that will had a single \P\Iris\Message
object or the \P\Iris\Batch::addMany
that will accept an array
or any Traversable
object that contained only \P\Iris\Message
objects.
At any given time you can count the number of \P\Iris\Message
objects present in the \P\Iris\Batch
object. This number will decrease as the object performs the requests.
Performing the requests
Once you have added all the \P\Iris\Message
objects you can safely perform you parallel requests using the \P\Iris\Batch::execute
method.
When using the \P\Iris\Batch
you must use \P\Iris\Message
event listeners to process each cURL
request individually.
Contribution
Please see CONTRIBUTING for details.
Credits
All versions of iris with dependencies
ext-curl Version *