Installation
composer require apis-guru/openapi-directory
(Note: This package is primarily a directory of OpenAPI specs, not a PHP library. The actual specs are fetched dynamically via HTTP.)
Fetching an API Spec
use ApisGuru\OpenApiDirectory\Client;
$client = new Client();
$spec = $client->getSpec('swagger-petstore'); // Returns OpenAPI 3.x spec as array
First Use Case: Validate a Request
$validator = new \ApisGuru\OpenApiDirectory\Validator($spec);
$isValid = $validator->validateRequest('/pets', 'get', ['query' => ['limit' => 10]]);
swagger-petstore, github, stripe).// 1. Fetch spec once (cache it)
$spec = $client->getSpec('stripe');
// 2. Use in a Laravel middleware/service
public function handle(Request $request, Closure $next) {
$validator = new Validator($spec);
if (!$validator->validateRequest($request->path(), $request->method(), $request->all())) {
abort(400, 'Invalid API request per Stripe spec');
}
return $next($request);
}
// Convert spec to OpenAPI JSON for Swagger UI
$jsonSpec = json_encode($spec, JSON_PRETTY_PRINT);
file_put_contents(storage_path('app/swagger.json'), $jsonSpec);
// Mock API responses based on OpenAPI spec
$mockResponse = $validator->generateMockResponse('/pets', 'get');
$this->assertEquals(200, $mockResponse['status']);
// Cache specs for 1 hour (TTL)
$spec = Cache::remember("openapi_{$apiName}", now()->addHour(), function() use ($client, $apiName) {
return $client->getSpec($apiName);
});
// Register API validator as a singleton
public function register() {
$this->app->singleton('openapi-validator', function ($app) {
$spec = $app['apis.guru.client']->getSpec(config('services.api.name'));
return new Validator($spec);
});
}
Rate Limiting
apis.guru service may throttle requests. Cache specs aggressively.Cache::forever() for static APIs (e.g., swagger-petstore).Spec Version Mismatches
OpenAPI 2.0 (Swagger) vs. 3.x. Validate with:
if (isset($spec['swagger'])) { // OpenAPI 2.0
// Handle legacy spec
}
Dynamic Path Parameters
{id} placeholders. Normalize paths before validation:
$normalizedPath = str_replace('/{id}', '', $request->path());
Authentication Headers
Authorization). Add middleware:
if (!$validator->validateRequest($path, $method, $data) ||
!$this->validateAuthHeaders($request)) {
abort(401);
}
dd($client->getSpec('github')); // Debug full spec structure
twitter) may be outdated. Verify with the API provider.Custom Spec Fetching
Override Client to fetch from a private registry:
class PrivateApiClient extends Client {
protected function fetchSpec($apiName) {
return file_get_contents("https://your-registry.com/{$apiName}.json");
}
}
Schema Validation
Extend Validator to add custom rules:
$validator->addRule('/pets/{id}', 'post', function ($data) {
return strlen($data['name']) > 3; // Custom rule
});
Webhook Validation Use the validator for webhook payloads:
$validator->validateResponse('/webhooks', 'post', $request->json()->all());
$client = new Client([
'cache_dir' => storage_path('openapi_cache'),
'timeout' => 10,
]);
$requestPath = strtolower($request->path());
How can I help you explore Laravel packages today?