CurlWrapper - the simplest OOP wrapper for PHP cURL

My scripts 16:00 / 10.06.2021 29

About CurlWrapper library

CurlWrapper – the simplest OOP wrapper for PHP cURL with no overhead.
Designed to test and work with simple RestFull and JSON APIs and execute simple queries.

No advanced settings, just a minimum set of options needed to initialize cURL and perform simple queries with different headers. Writing similar code can be done in minutes, but sometimes you want to have a ready-made library for simple tests so you don't waste time.

To install using Composer, use the command:

$ composer require icemont/curlwrapper

Usage Examples

Simple POST and GET requests
$curl = new CurlWrapper();

// Executing an empty POST request
var_dump($curl->request('http://example.com/post', true));

// Add data and execute a POST request with data sending
$curl->addParam('data', array('foo' => 'bar'));
$curl->addParam('int', 1);
var_dump($curl->request('http://example.com/post'));

// Reset the data and execute a simple GET request
$curl->reset();
var_dump($curl->request('http://example.com/'));

 

Example of new ticket creation via the osTicket API
File: examples/osticket_create_ticket.php
$api = new CurlWrapper();

// Adding named query parameters one by one
$api->addHeader('X-API-Key: YOUR_API_KEY');
$api->addParam('alert', true);
$api->addParam('autorespond', true);
$api->addParam('source', 'API');
$api->addParam('name', 'Test User');
$api->addParam('email', '[email protected]');
$api->addParam('ip', '127.0.0.1');

// Or immediately in the array
$params = ['subject' => 'Testing API', 'message' => 'MESSAGE HERE'];
$api->addParam(false, $params);

// Executing the query as JSON
var_dump($api->jsonRequest('https://support.example.com/api/tickets.json'));

// Print the response code
echo 'Request response code: ' . $api->httpcode . PHP_EOL;
// Print the error string
echo 'Request error string: ' . $api->lasterror . PHP_EOL;