dvelopment/curl
dvelopment/curl is a lightweight PHP wrapper around cURL for making HTTP requests with a simpler API. Provides helpers for common request types, headers, query params, and response handling, suitable for quick integrations and small services.
Installation Add the package via Composer:
composer require dvelopment/curl
Register the service provider in config/app.php under providers:
Dvelopment\Curl\CurlServiceProvider::class,
First Use Case Make a simple GET request:
use Dvelopment\Curl\Curl;
$response = Curl::get('https://api.example.com/data');
echo $response->body;
Where to Look First
README.md for basic usage.Curl facade (Dvelopment\Curl\Facades\Curl) is the primary entry point.php artisan vendor:publish --provider="Dvelopment\Curl\CurlServiceProvider"
Basic Requests Use fluent methods for common HTTP verbs:
$response = Curl::post('https://api.example.com/submit', [
'key' => 'value'
]);
Headers and Options Pass headers or cURL options as arrays:
$response = Curl::withHeaders(['Authorization' => 'Bearer token'])
->withOptions(['CURLOPT_TIMEOUT' => 30])
->get('https://api.example.com/secure');
Authentication Handle basic auth or OAuth:
$response = Curl::withAuth('username', 'password')
->get('https://api.example.com/protected');
Response Handling Access response data via properties:
$response = Curl::get('https://api.example.com/data');
$status = $response->status; // HTTP status code
$body = $response->body; // Response body
$headers = $response->headers; // Response headers
Error Handling
Use exceptions or check the success property:
try {
$response = Curl::get('https://api.example.com/fail');
} catch (\Dvelopment\Curl\Exceptions\CurlException $e) {
// Handle error
}
API Integration
class GitHubService {
public function fetchUser($username) {
return Curl::get("https://api.github.com/users/{$username}");
}
}
Background Jobs Offload cURL requests to queues (e.g., Laravel Queues):
use Illuminate\Support\Facades\Queue;
Queue::push(function () {
Curl::post('https://api.example.com/async', ['data' => 'value']);
});
Middleware Add request/response middleware for logging or transformations:
Curl::addMiddleware(function ($request, $next) {
$request->headers['X-Custom-Header'] = 'value';
return $next($request);
});
Http client for hybrid workflows.Curl::fake() (if supported).config/curl.php.Resource Leaks
Curl instances are properly closed (the package likely handles this, but verify).SSL/Warnings
Curl::withOptions(['CURLOPT_VERBOSE' => false, 'CURLOPT_SSL_VERIFYPEER' => false]);
Header Conflicts
Content-Type or User-Agent may override defaults. Explicitly set them if needed:
Curl::withHeaders(['Content-Type' => 'application/json']);
Large Responses
$response = Curl::withOptions(['CURLOPT_WRITEFUNCTION' => function($ch, $data) {
// Process data incrementally
}])->get('https://api.example.com/large-data');
Enable Verbose Output For debugging, enable cURL verbose mode:
Curl::withOptions(['CURLOPT_VERBOSE' => true]);
Check Laravel logs for output.
Check Response Codes
Always inspect $response->status for non-200 responses.
Timeouts Set reasonable timeouts (default may be too low):
Curl::withOptions(['CURLOPT_TIMEOUT' => 60]);
config/curl.php:
'user_agent' => 'MyApp/1.0',
Curl::withOptions([
'CURLOPT_PROXY' => 'http://proxy.example.com:8080',
]);
Custom Responses
Extend the Dvelopment\Curl\Response class to add methods:
class CustomResponse extends \Dvelopment\Curl\Response {
public function parseJson() {
return json_decode($this->body, true);
}
}
Bind it in the service provider.
Hooks Use middleware or events (if the package supports them) to intercept requests/responses.
Low-Level Access For advanced use cases, access the underlying cURL handle:
$handle = Curl::getHandle();
// Manipulate handle directly (use with caution)
How can I help you explore Laravel packages today?