ixudra/curl
ixudra/curl is a fluent, Laravel-friendly PHP cURL wrapper for building and sending HTTP requests. Configure options with a query-builder-like API, use handy helpers for common settings, and integrate easily via service provider/facade (framework-independent core).
Installation:
composer require ixudra/curl
Laravel 5.5+ auto-discovers the package; for older versions, register the service provider and facade in config/app.php.
First Use Case: Send a simple GET request:
use Ixudra\Curl\Facades\Curl;
$response = Curl::to('https://api.example.com/data')->get();
echo $response;
Key Entry Points:
Curl::to('url')->method() (e.g., get(), post()).withData(), withHeader(), asJson(), etc.returnResponseObject() for structured data.RESTful Requests: Chain methods for HTTP verbs and payloads:
Curl::to('https://api.example.com/users/1')
->withData(['name' => 'John'])
->asJson()
->put();
Authentication: Use utility methods for common auth headers:
Curl::to('https://api.example.com/protected')
->withBearer('token123')
->get();
File Uploads: Attach files to POST requests:
Curl::to('https://api.example.com/upload')
->withData(['user_id' => 1])
->withFile('avatar', '/path/to/avatar.jpg', 'image/jpeg', 'avatar.jpg')
->post();
Response Processing: Handle structured responses:
$response = Curl::to('https://api.example.com/data')
->returnResponseObject()
->withResponseHeaders()
->get();
if ($response->status === 200) {
$data = json_decode($response->content, true);
}
Error Handling: Debug failed requests:
try {
$response = Curl::to('https://api.example.com/fail')
->enableDebug(storage_path('logs/curl_debug.log'))
->get();
} catch (\Exception $e) {
// Log or handle error
}
$app->singleton('curl.config', fn() => ['timeout' => 30]);
Curl::setConfig($app->make('curl.config'));
Curl::macro('withProjectHeaders', function () {
return $this->withHeaders([
'X-Project-ID' => config('app.project_id'),
'Accept' => 'application/json',
]);
});
Http::fake() or use Curl::shouldReceive() in PHPUnit.JSON Conflicts:
asJson() with file uploads corrupts multipart requests.asJson() for file uploads; use raw form-data instead.Header Overrides:
Content-Type) may silently override each other.withHeaders() with an associative array to ensure clarity:
->withHeaders(['Content-Type' => 'application/json', 'Accept' => 'application/json'])
Proxy Authentication:
Curl::setConfig(['proxy' => ['url' => 'proxy.example.com', 'auth' => ['user', 'pass']]]);
Response Object Quirks:
withResponseHeaders() requires returnResponseObject().->returnResponseObject()
->withResponseHeaders()
Debug Logging:
->enableDebug(storage_path('logs/curl_debug_'.now()->timestamp.'.log'))
Inspect Raw cURL Command:
Use enableDebug() to generate a cURL command for manual testing:
curl -X GET "https://api.example.com/data" -H "Authorization: Bearer token123"
Check HTTP Status Codes:
Always validate $response->status (or raw response for simple requests):
if (str_starts_with($response, '{"error"')) {
// Handle error
}
Timeout Handling: Set a default timeout in config or per request:
Curl::setConfig(['timeout' => 10]); // Global
// OR
->withOption(CURLOPT_TIMEOUT, 10); // Per request
SSL/TLS Issues: Disable verification for testing (not production):
->withOption(CURLOPT_SSL_VERIFYPEER, false)
->withOption(CURLOPT_SSL_VERIFYHOST, false)
Custom Options: Add project-specific cURL options via macros:
Curl::macro('withCustomOption', function ($option, $value) {
return $this->withOption($option, $value);
});
// Usage:
->withCustomOption(CURLOPT_FOLLOWLOCATION, true)
Response Transformers:
Extend the CurlService to auto-transform responses:
class ExtendedCurlService extends \Ixudra\Curl\CurlService {
public function get() {
$response = parent::get();
return json_decode($response, true);
}
}
Event Listeners: Trigger events before/after requests:
Curl::extend(function ($builder) {
$builder->before(function ($request) {
// Log request details
});
});
How can I help you explore Laravel packages today?