- How do I install the Cloudways PHP SDK in a Laravel project?
- Run `composer require christhompsontldr/phpsdk` in your project directory. The SDK supports Laravel integration via `.env` variables (`CW_EMAIL` and `CW_API_KEY`) for authentication. Ensure your `composer.json` includes the correct package name to avoid conflicts with the older `cloudwaysapi/phpsdk` package.
- Does this SDK support Laravel’s service container and facades?
- Yes, the SDK’s stateless design allows easy binding to Laravel’s IoC. You can create a facade (e.g., `Cloudways::createServer()`) or bind it directly in `config/app.php` for cleaner usage. Example: `bind(Server::class, fn() => new Server(config('cloudways.email'), config('cloudways.key')))`.
- Can I use this SDK for async operations like server provisioning in Laravel queues?
- Absolutely. The SDK’s `getOperationResult($operationId, $wait)` method can be wrapped in a Laravel Job with `shouldQueue(true)`. For example, create a `ProvisionServerJob` that calls `$server->create_server($params)` and uses the SDK’s async helpers to poll for completion.
- What Laravel versions does this SDK support, and are there any dependencies?
- The SDK is compatible with Laravel 7+ and requires PHP 7.4+. It depends on Guzzle for HTTP requests, which may conflict with Laravel’s built-in `HttpClient`. Use Laravel’s `HttpClient` facade directly or alias the SDK’s Guzzle instance to resolve conflicts.
- How do I handle API key rotation in production without hardcoding credentials?
- Store your Cloudways API key and email in Laravel’s `.env` file (e.g., `CW_EMAIL` and `CW_API_KEY`). Rotate keys by updating these variables and restarting your Laravel application. Avoid hardcoding credentials in your codebase to follow security best practices.
- Does the SDK support testing with mocked Cloudways API responses?
- The SDK lacks built-in test mocks, but you can use tools like Mockery or VCR for API recording to simulate Cloudways API responses in Laravel tests. For example, mock the `Server` class methods (e.g., `create_server`) to return predefined responses during testing.
- Are there alternatives to this SDK for managing Cloudways resources in Laravel?
- While this SDK is the official Cloudways PHP SDK, alternatives include building a custom wrapper around the Cloudways API using Laravel’s `HttpClient` or using third-party packages like `spatie/laravel-http-client` for more control. However, this SDK provides convenient wrappers for common actions like server management and async tasks.
- How do I handle rate limits or API errors in production?
- The SDK does not include built-in retry logic for rate limits. Use Laravel’s `Retryable` trait or `spatie/laravel-queueable` to implement retries for failed API calls. For custom error handling, catch exceptions and map them to Laravel-specific exceptions (e.g., `CloudwaysApiException`).
- Can I use this SDK to listen to Cloudways webhooks for real-time events?
- No, the SDK does not support webhook listening. To handle real-time events, you’ll need to implement a separate service (e.g., a Laravel queue worker) that polls the Cloudways API for changes or uses a webhook receiver like Laravel’s `HandleIncomingWebhook` trait.
- How do I handle long-running operations like server creation without timing out?
- Use the SDK’s `getOperationResult($operationId, $wait)` method to poll for operation completion. For Laravel, wrap this in a Job with `shouldQueue(true)` and set a reasonable `$wait` value (e.g., 30 seconds). If PHP’s max execution time is reached, re-run the job until the operation completes.