adrii / curl-helper
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.
Installs: 15 219
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- ext-curl: *
Requires (Dev)
- phpunit/phpunit: ^9.5
README
Original author: Adrii
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.
Features
- Versatile HTTP Support: GET, POST, PUT, and DELETE methods are all supported, catering to a wide range of API interactions.
- Header & Option Customization: Effortlessly configure request headers and cURL options to meet the precise needs of every API call.
- File Uploads: Simplified multipart/form-data handling makes file uploads a breeze.
- Data Flexibility: Easily switch between raw and array data inputs, with automatic content type handling for JSON and form-urlencoded data.
- Debug Mode: Activate detailed logging for troubleshooting and optimization, gaining insight into request and response cycles.
Requirements
- PHP 7.4 or higher
- cURL extension enabled in PHP
Installation
You can copy the CurlHelper
class into your project, or preferably, autoload it using Composer.
composer require adrii/curl-helper
Composer
use Adrii\CurlHelper;
Manual
require_once ROOT . 'CurlHelper.php';
Usage
$curl = new CurlHelper();
Methods
This section provides a quick start for users to understand how to apply the CurlHelper
class in their projects for various HTTP requests, setting custom headers, and enabling debug mode for detailed request and response insights.
GET Request
$curl->setUrl('http://example.com/api/data'); $curl->setGetParams(['key' => 'value']); $curl->execute(); $response = $curl->response();
POST Request
$curl->setUrl('http://example.com/api/data'); $curl->setPostParams(['key' => 'value']); $curl->execute(); $response = $curl->response();
PUT Request
$curl = new CurlHelper(); $curl->setUrl('https://example.com/api/data/1'); $curl->setPutParams(['key' => 'updatedValue']); $curl->execute(); $response = $curl->response();
DELETE Request
$curl = new CurlHelper(); $curl->setUrl('https://example.com/api/data/1'); $curl->execute(); $response = $curl->response();
Setting Custom Headers
$curl->setUrl('http://example.com/api/data'); $curl->setPostParams(['key' => 'value']); $curl->execute(); $response = $curl->response();
Enabling Debug Mode
$curl->setDebug(); $curl->execute(); $debugInfo = $curl->debug();
Handling Errors
The execute
method throws an exception if the cURL request fails. Make sure to catch these exceptions and handle them accordingly.
try { $curl->execute(); } catch (\Exception $e) { echo "Error: " . $e->getMessage(); }
Example
These examples cover basic usage scenarios for CurlHelper. Modify parameters and URLs as needed to fit your specific use case.
$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();
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
CurlHelper is open-sourced software licensed under the MIT