Owl is a simply cURL wrapper for PHP, (*1)
, (*2)
Requirements
- PHP >= 5.6
-
cURL library enabled
Install
Composer
{
"require": {
"pablosanches/owl": "1.0.*"
}
}
require_once './vendor/autoload.php';
Usage
// Namespace shortcut
use Owl\Method as Http;
// Template
$request = new Http\<METHOD_NAME>( string $url [, array $options ] );
Methods are:, (*3)
Get()
Head()
Options()
Post()
Put()
Patch()
Delete()
Constructor $options
[
'data' => [ // Data to send, available for `Post`, `Put` and `Patch`
'foo' => 'bar'
],
'headers' => [ // Additional headers (optional)
'Authorization: foobar'
],
'ssl' => '/cacert.pem', // Use it for SSL (optional)
'is_payload' => true, // `true` for sending a payload (JSON-encoded data, optional)
'autoclose' => true // Is the request must be automatically closed (optional)
]
Public methods
// Send a request
$request->send();
// HTTP status code
$request->getStatus();
// HTTP header
$request->getHeader();
// HTTP body response
$request->getResponse();
// Used cURL options
$request->getCurlOptions();
// Set a cURL option
$request->setCurlOption(CURLOPT_SOMETHING, $value);
// Manually close the handle (necessary when `autoclose => false` is used)
$request->close();
Examples
Basic:, (*4)
// Namespace shortcut
use Owl\Method as Http;
// Standard GET request
$request = new Http\Get('http://domain.com');
// Send this request
$request->send();
echo $request->getResponse(); // body response
echo $request->getStatus(); // HTTP status code
Send a payload:, (*5)
// Namespace shortcut
use Owl\Method as Http;
// JSON-encoded POST request
$request = new Http\Post($this->endpoint, [
'data' => [
'name' => 'foo',
'email' => 'foo@domain.com'
],
// With 'is_payload' => true
// You don't have to json_encode() your array of data
// Moreover, the appropriate headers will be set for you
'is_payload' => true
]);
// Send this request
$request->send();
echo $request->getResponse(); // body response
echo $request->getStatus(); // HTTP status code
Manual closing:, (*6)
// Namespace shortcut
use Owl\Method as Http;
// Set `autoclose` option to `false`
$request = new Http\Get('http://domain.com', [
'autoclose' => false
]);
// Send this request
$request->send();
// Now you can retrieve a cURL info as the handle is still open
$request->getCurlInfo(CURLINFO_SOMETHING);
echo $request->getResponse();
// Manually close the handle
$request->close();
Tests
On project directory:, (*7)
-
composer install to retrieve phpunit
-
phpunit to run tests