Installation
composer require api-check/php-client
Ensure your project meets PHP 8.1+ requirements.
First Use Case: API Health Check
use ApiCheck\Client;
$client = new Client('https://api.example.com');
$response = $client->check();
if ($response->isHealthy()) {
echo "API is healthy!";
} else {
echo "API issues detected: " . $response->getErrors();
}
Key Files to Explore
src/Client.php – Core client logic.src/Response.php – Response handling and validation.tests/ – Example use cases and edge cases.Service Provider Binding
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(Client::class, function ($app) {
return new Client(config('services.api_check.endpoint'));
});
}
Configuration
// config/services.php
'api_check' => [
'endpoint' => env('API_CHECK_ENDPOINT', 'https://api.example.com'),
'timeout' => 5.0,
'headers' => [
'Authorization' => 'Bearer ' . env('API_CHECK_TOKEN'),
],
],
Middleware for API Guarding
// app/Http/Middleware/CheckApiHealth.php
public function handle($request, Closure $next)
{
$client = app(Client::class);
$response = $client->check();
if (!$response->isHealthy()) {
return response()->json(['error' => 'API Unavailable'], 503);
}
return $next($request);
}
Scheduled Monitoring
// app/Console/Commands/MonitorApi.php
use Illuminate\Console\Command;
use ApiCheck\Client;
class MonitorApi extends Command
{
protected $signature = 'api:monitor';
public function handle()
{
$client = new Client(config('services.api_check.endpoint'));
$response = $client->check();
if (!$response->isHealthy()) {
$this->error("API Unhealthy: " . $response->getErrors());
// Send alert (e.g., Slack, Email)
}
}
}
Timeout Handling
$client = new Client('https://api.example.com', ['timeout' => 10.0]);
try {
$response = $client->check();
} catch (ConnectException $e) {
Log::error("API Connection Failed: " . $e->getMessage());
}
Response Parsing Quirks
healthy and errors fields.Response class to handle custom formats:
class CustomResponse extends Response
{
public function isHealthy(): bool
{
return $this->data['status'] === 'ok';
}
}
Rate Limiting
use Symfony\Component\Process\Exception\TimeoutException;
try {
$response = $client->check();
} catch (TimeoutException $e) {
sleep(2); // Retry after delay
retry();
}
HTTPS/SSL Issues
$client = new Client('https://api.example.com', [
'verify_peer' => false,
]);
trusted_proxies or config/cors.php for proxy setups.Custom Endpoints
Override the default /health endpoint:
$client = new Client('https://api.example.com', ['endpoint' => '/custom-health']);
Adding Metrics Integrate with Laravel Telescope or Prometheus:
$response = $client->check();
\Prometheus\CollectorRegistry::default()->getOrRegisterCounter(
'api_health_checks_total',
'Total API health checks',
['status' => $response->isHealthy() ? 'healthy' : 'unhealthy']
)->inc();
Mocking for Tests Use Laravel’s HTTP mocking:
use Illuminate\Support\Facades\Http;
Http::fake([
'https://api.example.com/health' => Http::response(['healthy' => true]),
]);
$this->assertTrue($client->check()->isHealthy());
How can I help you explore Laravel packages today?