carlescliment/curl
Lightweight PHP cURL wrapper by carlescliment. Simplifies making HTTP requests with an easy API for GET/POST and custom headers, options, and timeouts, returning response data and status info for quick integrations and scripting.
Installation:
composer require carlescliment/curl
Add the service provider to config/app.php under providers:
CarlesCliment\Curl\CurlServiceProvider::class,
Basic Usage:
The package provides a fluent interface for making HTTP requests. Start by resolving the Curl facade:
use CarlesCliment\Curl\Facades\Curl;
$response = Curl::to('https://api.example.com/data')
->get()
->send();
First Use Case: Fetch JSON data from an API:
$data = Curl::to('https://api.example.com/users')
->withHeader('Accept', 'application/json')
->get()
->json();
Chaining Requests:
$response = Curl::to('https://api.example.com')
->withHeader('Authorization', 'Bearer token')
->post()
->withData(['key' => 'value'])
->send();
Handling Responses:
$response = Curl::to('https://api.example.com/data')->get()->send();
if ($response->isOk()) {
$body = $response->body();
$json = $response->json();
}
Error Handling:
try {
$response = Curl::to('https://api.example.com')->get()->send();
$response->throwIfNotOk();
} catch (\CarlesCliment\Curl\Exceptions\CurlException $e) {
// Handle error
}
Reusing Configurations:
// In a service or helper
function fetchWithAuth($url, $data) {
return Curl::to($url)
->withHeader('Authorization', 'Bearer ' . auth()->token())
->post()
->withData($data)
->send();
}
Integration with Laravel HTTP Client: For consistency, mirror Laravel's HTTP client patterns:
$response = Curl::to('https://api.example.com')
->withOptions(['timeout' => 30])
->withHeader('X-Custom-Header', 'value')
->asJson()
->get();
No Built-in Retry Mechanism: Unlike Laravel's HTTP client, this package lacks retry logic. Implement manually:
$attempts = 3;
while ($attempts--) {
try {
$response = Curl::to(url)->get()->send();
break;
} catch (\Exception $e) {
if ($attempts === 0) throw $e;
sleep(1);
}
}
No Automatic JSON Parsing:
Always explicitly call ->json() to decode responses:
// ❌ Won't work as expected
$data = Curl::to(url)->get()->send()->json;
// ✅ Correct
$data = Curl::to(url)->get()->send()->json();
Curl Options Override:
Custom CURLINFO_* options may not persist across requests. Reset them if needed:
Curl::resetOptions();
Enable Verbose Output:
Curl::to(url)->withOption(CURLOPT_VERBOSE, true)->get()->send();
Logs will appear in stderr.
Inspect Headers:
$headers = Curl::to(url)->get()->send()->headers();
Check Effective URL:
$finalUrl = Curl::to(url)->get()->send()->effectiveUrl();
Custom Middleware: Attach middleware to modify requests/responses:
Curl::to(url)->middleware(function ($request) {
$request->withHeader('X-Middleware', 'enabled');
})->get()->send();
Override Default Options:
Configure defaults in config/curl.php:
'defaults' => [
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false, // ⚠️ Disable only for testing!
],
Extend Response Class:
Create a custom response handler by extending CarlesCliment\Curl\Response:
class CustomResponse extends \CarlesCliment\Curl\Response {
public function customMethod() {
return $this->body() . ' (custom)';
}
}
Bind it in the service provider’s boot method.
$handle = Curl::create()->to(url);
$handle->get()->send();
$handle->post()->withData($data)->send();
How can I help you explore Laravel packages today?