alexkart/curl-builder
Generate reproducible curl commands from PSR-7 ServerRequest instances or build them manually. Add, set, and override options (with or without arguments), set URLs, and output a ready-to-run curl string—useful for debugging HTTP requests and sharing examples.
## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require alexkart/curl-builder
Add to composer.json if not using Composer directly.
First Use Case:
Generate a basic GET request:
use Alexkart\CurlBuilder\CurlBuilder;
$curl = CurlBuilder::get('https://api.example.com/users')
->withHeader('Accept', 'application/json')
->build();
echo $curl; // Outputs the full cURL command
Where to Look First:
CurlBuilder class methods (e.g., get(), post(), withHeader(), withData()).build() method to generate the final command.API Requests:
$curl = CurlBuilder::post('https://api.example.com/users')
->withHeader('Authorization', 'Bearer token123')
->withData(['name' => 'John', 'email' => 'john@example.com'])
->withOption(CURLOPT_RETURNTRANSFER, true)
->build();
Note: Duplicate options are now handled gracefully (fixed in 1.1.0).
Authentication:
$curl = CurlBuilder::get('https://api.example.com/protected')
->withBasicAuth('username', 'password')
->build();
File Uploads:
$curl = CurlBuilder::post('https://api.example.com/upload')
->withFile('file.txt', '/path/to/file.txt')
->build();
Integration with Laravel HTTP Client:
$curlCommand = CurlBuilder::get('https://api.example.com/data')->build();
$response = Http::withOptions(['curl' => $curlCommand])->get('/');
class ApiClient {
public static function getUsers() {
return CurlBuilder::get('https://api.example.com/users')
->withHeader('Accept', 'application/json');
}
}
->debug() to inspect intermediate steps.
$curl = CurlBuilder::get('https://api.example.com/data')->debug();
Option Conflicts:
CURLOPT_URL) manually after setting the URL.withOption() for custom options, but ensure they don’t clash with defaults.File Paths:
withFile(). Relative paths may fail silently.Encoding Issues:
$url = urlencode('https://api.example.com/search?q=hello world');
$curl = CurlBuilder::get('https://api.example.com/data')->build();
echo $curl; // Paste into terminal to test manually.
withHeader (not withHeaders).CURLOPT_RETURNTRANSFER, not CURLOPT_RETURN_TRANSFER).Custom Options: Add frequently used options as methods:
$curl->withFollowRedirects(true); // Alias for CURLOPT_FOLLOWLOCATION
Extend the CurlBuilder class or create a trait.
Middleware: Chain middleware for pre-processing requests:
$curl = CurlBuilder::get('https://api.example.com/data')
->pipe(function ($builder) {
$builder->withHeader('X-Custom-Header', 'value');
});
Testing: Mock the builder in unit tests:
$mockBuilder = Mockery::mock(CurlBuilder::class);
$mockBuilder->shouldReceive('build')->andReturn('mocked_curl_command');
Note: Test coverage has been improved in 1.1.0, making edge cases easier to validate.
PHP 8.4 Features:
#[Attribute] for metadata in custom builders.
NO_UPDATE_NEEDED would **not** apply here—this assessment has been updated to reflect the new release's fixes and improvements.
How can I help you explore Laravel packages today?