/CurlHelper

CurlHelper is a streamlined PHP utility for easy cURL usage, supporting GET, POST, PUT, and DELETE methods. It simplifies HTTP requests by offering straightforward options and header settings, making web API interactions more accessible and efficient.

Primary LanguagePHPMIT LicenseMIT

CurlHelper

By Adrii

Test Latest Stable Version Total Downloads License

CurlHelper is a lightweight PHP class to organize and have a standard in projects.

Installation

Use Composer to install the library.

composer require adrii/curl-helper

Composer

use Adrii\CurlHelper;

Manual

require_once ROOT . 'CurlHelper.php';

Usage

$url    = '{url}';
$basic  = '{basic}';

$curl = new CurlHelper();

$curl->setUrl($url);

$curl->setPostParams([
    'param' => '{param}',
    'type' => '{type}'
]);

$curl->setHeaders([
    "Authorization" => "Basic {$basic}"
], false); // Disable parse (dafault Enabled:true) 

$curl->setOptions([
    CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_2_0,
    CURLOPT_CONNECTTIMEOUT => 30,
]);

$curl->setMime("json");

$curl->setUtf8();

$curl->execute();

$response   = $curl->response();
$code       = $curl->http_code();

list($error, $msg) = $curl->parseCode();

Methods

POST Request

$curl->setPostParams([
    'client_id' => '{app-id}',
    'client_secret' => '{app-secret}',
    'grant_type' => 'authorization_code',
    'redirect_uri' => '{redirect-uri}',
    'code' => '{code}'
]);
// Array
$curl->setPostRaw([
    'client_id' => '{app-id}',
    'client_secret' => '{app-secret}',
    'grant_type' => 'authorization_code',
    'redirect_uri' => '{redirect-uri}',
    'code' => '{code}'
]);

// String
$curl->setPostRaw("key1=value1&key2=value2");

GET Request

$curl->setGetParams([
    'param' => '{param}',
    'type' => '{type}'
]);

PUT Request

$curl->setPutParams([
    'param' => '{param}',
    'type' => '{type}'
]);

DELETE Request

$curl->setDeleteParams([
    'param' => '{param}',
    'type' => '{type}'
]);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT