- How do I install mosparo/php-api-client in a Laravel project?
- Run `composer require mosparo/php-api-client` to install the package. Since it has no Laravel-specific dependencies, you’ll need to manually configure it using Laravel’s HTTP client (e.g., `Http::withToken()`) or Guzzle. Store your Mosparo API key in `.env` and reference it in your configuration.
- Does this package support Laravel’s service container for dependency injection?
- Yes, you can bind the Mosparo client to Laravel’s container manually. For example, add a service provider or use the container’s `singleton` method to resolve the client with your API key from config. This enables clean DI in controllers or services.
- What Laravel versions are compatible with mosparo/php-api-client?
- The package requires PHP 8.0+, so it works with Laravel 8.x, 9.x, and 10.x. Test thoroughly in your target Laravel version, as the package itself doesn’t enforce Laravel-specific constraints beyond HTTP client compatibility.
- How do I handle API errors or rate limits from Mosparo in Laravel?
- The package doesn’t include built-in retry logic, but you can wrap it in Laravel middleware or use Guzzle’s retry middleware. For rate limits (e.g., 429 errors), implement exponential backoff in a custom service layer or leverage Laravel’s `Http::timeout()` and `retry()` methods.
- Can I use this package asynchronously in Laravel queues or Horizon?
- No, the package is synchronous by design. To use it in queues, dispatch a job that instantiates the client and processes the request. Avoid blocking queue workers by offloading API calls to a separate microservice if high throughput is needed.
- How do I secure Mosparo API keys in a Laravel application?
- Store the API key in Laravel’s `.env` file (e.g., `MOSPARO_API_KEY`). For production, use Laravel Vault or environment variable encryption. Never hardcode keys in configuration files or version-controlled directories.
- Are there Laravel-specific integrations, like Form Request validation or middleware?
- The package itself doesn’t include Laravel middleware or Form Request validation, but you can create custom middleware to validate Mosparo responses before processing form submissions. For example, add a `VerifyMosparoToken` middleware to your form routes.
- What happens if Mosparo’s API changes or deprecates endpoints?
- The package lacks built-in versioning, so API changes may break your integration. Monitor Mosparo’s API documentation and update the package or fork it if needed. Consider adding feature flags or a wrapper layer to isolate Mosparo-specific logic.
- How do I test this package in Laravel unit tests?
- Mock the HTTP client (e.g., `Http::fake()` or Guzzle’s mock handler) to simulate Mosparo API responses. Test edge cases like failed CAPTCHAs, invalid tokens, and network errors. Use Laravel’s `Http` facade or a custom service class for easier mocking.
- What are the alternatives to mosparo/php-api-client for Laravel spam protection?
- Consider Laravel-specific packages like `spatie/laravel-honeypot` for basic bot detection or `google/recaptcha` for reCAPTCHA integration. For API-based solutions, evaluate `spatie/laravel-activitylog` with custom spam filters or dedicated services like Akismet’s PHP SDK.