The Overseas Bundle simplifies HTTP client interactions in Laravel by providing a fluent, chainable API wrapper. To get started, install via Composer:
composer require answear/overseas-bundle
Publish the config (if needed) and register the service provider in config/app.php. The core feature is the Overseas facade, which allows sending HTTP requests with minimal boilerplate:
use Answear\Overseas\Facades\Overseas;
// Basic GET request
$response = Overseas::get('https://api.example.com/users');
// With query parameters
$response = Overseas::get('https://api.example.com/users', ['active' => true]);
First Use Case: Fetching and parsing JSON responses with automatic decoding:
$data = Overseas::get('https://api.example.com/data')->json();
Request Chaining: Build requests fluently:
$response = Overseas::get('https://api.example.com/posts/1')
->withHeaders(['Authorization' => 'Bearer token'])
->asJson()
->send();
Response Handling: Use methods like json(), text(), or status():
$status = Overseas::get('https://api.example.com/health')->status();
Error Handling: Leverage Laravel’s exception handling or use ->throw():
try {
$response = Overseas::post('https://api.example.com/login')->throw();
} catch (\Answear\Overseas\Exceptions\OverseasException $e) {
// Handle error
}
'middleware' => [
\App\Http\Middleware\AddCustomHeader::class,
],
Overseas::macro('authenticate', function () {
return $this->withHeaders(['Authorization' => 'Bearer ' . auth()->token()]);
});
Overseas::fake() to mock responses:
Overseas::fake([
'https://api.example.com/users' => ['id' => 1, 'name' => 'Test'],
]);
The get() method (added in PR #22) allows direct access to the underlying Guzzle response object for advanced use cases:
$guzzleResponse = Overseas::get('https://api.example.com/data')->get();
$headers = $guzzleResponse->getHeaders();
->response() (use ->get() for raw responses or ->json()/->text() for parsed data).Accept: application/json).'debug' => true in config to log requests/responses.->get() to inspect raw headers if responses are malformed.->get() for server-side clues.Guzzle adapter by binding your own HTTP client to the answear.overseas.adapter service provider.\Answear\Overseas\Overseas::macro('successful', function () {
return $this->status() >= 200 && $this->status() < 300;
});
overseas.request and overseas.response events to log or modify requests/responses globally.'base_url' in config to avoid repeating full URLs.'timeout' (seconds) or override per request:
Overseas::get('...')->timeout(30);
How can I help you explore Laravel packages today?