The Veldora HTTP Client provides a clean, fluent interface for making outbound HTTP requests to external APIs and services. It is built on top of PHP's native cURL and streams, with support for JSON, authentication, retries, and fake responses in tests.
Basic Requests
example.php
PHP
use Veldora\Framework\Http\Client\Http;
// Simple GET request
$response = Http::get('https://api.github.com/users/veldorahq');
// GET with query parameters
$response = Http::get('https://api.example.com/posts', [
'page' => 1,
'per_page' => 20,
'status' => 'published',
]);
// POST with JSON body (auto sets Content-Type: application/json)
$response = Http::post('https://api.example.com/users', [
'name' => 'John Doe',
'email' => 'john@example.com',
]);
// PUT and DELETE
$response = Http::put('https://api.example.com/users/42', ['name' => 'Jane Doe']);
$response = Http::delete('https://api.example.com/users/42');Authentication
example.php
PHP
// Bearer token (OAuth 2.0, JWT, API keys)
$response = Http::withToken('your-api-token-here')
->get('https://api.example.com/protected-resource');
// Basic HTTP authentication
$response = Http::withBasicAuth('username', 'password')
->get('https://api.example.com/data');
// Custom headers
$response = Http::withHeaders([
'X-API-Key' => config('services.stripe.key'),
'X-Request-ID' => uniqid('req_'),
])->post('https://api.stripe.com/v1/charges', $payload);Working with Responses
example.php
PHP
$response = Http::get('https://api.github.com/repos/veldorahq/veldora');
// Check status
$response->successful(); // true if status 200-299
$response->failed(); // true if status 400+
$response->status(); // integer: 200, 404, 500, etc.
$response->ok(); // true if status 200
$response->notFound(); // true if status 404
$response->serverError(); // true if status 500+
// Read body
$data = $response->json(); // Decoded PHP array
$text = $response->body(); // Raw string
$header = $response->header('X-RateLimit-Remaining');
// Fluent conditional
if ($response->successful()) {
$repo = $response->json();
log_info('Repo fetched', ['stars' => $repo['stargazers_count']]);
} else {
log_error('GitHub API error', ['status' => $response->status()]);
}Request Options
example.php
PHP
// Accept JSON responses (sets Accept: application/json header)
$response = Http::acceptJson()->get('https://api.example.com/data');
// Send as form (application/x-www-form-urlencoded)
$response = Http::asForm()->post('https://api.example.com/login', [
'username' => 'admin',
'password' => 'secret',
]);
// Set a timeout (in seconds)
$response = Http::timeout(30)->get('https://slow-api.example.com/data');
// Follow redirects (enabled by default; pass false to disable)
$response = Http::withoutRedirecting()->get('https://api.example.com');Retries & Fault Tolerance
example.php
PHP
// Retry up to 3 times, waiting 500ms between attempts
$response = Http::retry(3, 500)->get('https://unstable-api.com/feed');
// Retry with exponential backoff
$response = Http::retry(5, 200)->post('https://api.example.com/data', $payload);Practical Example — Integrating a Payment Gateway
example.php
PHP
namespace App\Services;
use Veldora\Framework\Http\Client\Http;
class StripeService
{
public function createCharge(int $amountCents, string $cardToken): array
{
$response = Http::withBasicAuth(config('services.stripe.secret'), '')
->asForm()
->post('https://api.stripe.com/v1/charges', [
'amount' => $amountCents,
'currency' => 'usd',
'source' => $cardToken,
'description' => 'Veldora order',
]);
if ($response->failed()) {
$error = $response->json()['error']['message'] ?? 'Unknown error';
throw new \RuntimeException("Stripe charge failed: {$error}");
}
return $response->json();
}
}