Ever wanted to make an external request in Kohana and not to worry about the response thus speeding up your application? This is the module for you!
This module adds a Fire and Forget External Request Client which you can use when making external requests.
It utilises fsocketopen()
function which allows making a request without the need wait for a response, thus considerably speeding up your app's execution time.
You will find this module useful if your application makes simple API calls and its execution doesn't rely on the response returned.
- Allows "asynchronous" requests from PHP.
- Considerable speed increase compared to Kohana's native External Request Clients.
- Extends native Kohana's features, so you don't need to learn any new syntax.
- Full support for SSL requests.
- Checkout/download files and folders to
MODPATH/fafrequest
. - Add this entry under
Kohana::modules
array in yourAPPPATH/bootstrap.php
:
'fafrequest' => MODPATH.'fafrequest', // Fire and Forget Request
This module requires no configuration.
In order to use Fire and Forget Request Client you either have to specify it as a default one:
Kohana_Request_Client_External::$client = 'Request_Client_FireAndForget';
or you need to specify the client with each request (see examples below).
Request::factory('http://example.com/')
->client(new Request_Client_FireAndForget())
->execute();
Request::factory('http://example.com/example')
->client(new Request_Client_FireAndForget())
->query('foo', 'bar')
->query('baz', 'qux')
->execute();
NOTE: You always have to provide the GET parameters using Request::query()
method and not in the Request URI provided when calling Request::factory()
method.
Request::factory('http://example.com/submit')
->client(new Request_Client_FireAndForget())
->method(HTTP_Request::POST)
->post('foo', 'bar')
->post('baz', 'qux')
->execute();
Request::factory('http://example.com/submit')
->client(new Request_Client_FireAndForget())
->method(HTTP_Request::POST)
->post('foo', 'bar')
->post('baz', 'qux')
->query('corge', 'grault')
->query('garply', 'waldo')
->execute();
It is possible to set the fsocketopen()
timeout parameter.
The default value is 30 seconds.
Request::factory('http://example.com/')
->client(new Request_Client_FireAndForget(array('timeout' => 10)))
->execute();
You can make secure requests as simple as you would make a normal ones, just use https
scheme:
Request::factory('https://example.com/')
->client(new Request_Client_FireAndForget())
->execute();
Be aware that you can only use this module when making an External Request!
If an error occures when opening a socket the script will not stop the script's execution, instead it will log an error message. This is by design - after all it is "Fire and Forget".
The code for this module is losely based on W-Shadow's script and is inspired by my mate's question on StackOverflow.