- How do I install and use spatie/ssl-certificate in a Laravel project?
- Install via Composer with `composer require spatie/ssl-certificate`, then use `SslCertificate::createForHostName('example.com')` to fetch and validate certificates. The package requires the PHP OpenSSL extension, which is enabled by default in Laravel.
- Does this package support checking certificates for IPv6 addresses?
- Yes, the package supports IPv6 addresses. Use `SslCertificate::createForHostName('example.com', 443, 'ipv6')` to explicitly target IPv6. Test thoroughly in your environment, especially if behind proxies like Cloudflare.
- Can I validate certificates for non-standard ports (e.g., 8443) or custom socket contexts?
- Yes, pass the port as the second argument: `SslCertificate::createForHostName('example.com', 8443)`. For custom socket contexts (e.g., proxies), use the `createForHostName` method with a `stream_context_create` array as the fourth parameter.
- How do I handle self-signed certificates or certificates from untrusted CAs?
- The package doesn’t validate CA trust by default. Use `openssl_verify()` or `openssl_x509_checkpurpose()` in a wrapper to check trust. Log warnings for self-signed certs (common in staging) or disable verification with `$verifyCertificate = false` in `createForHostName`.
- Is there a way to cache certificate results to avoid repeated network calls?
- Yes, cache results using Laravel’s cache or Redis. Store the certificate data (e.g., `toArray()`) and refresh it periodically via a scheduled job. Example: `Cache::remember('cert_example_com', now()->addHours(1), fn() => SslCertificate::createForHostName('example.com'))`.
- How can I integrate this with Laravel’s scheduling for daily certificate checks?
- Create an Artisan command (e.g., `php artisan certificate:check`) and schedule it in `app/Console/Kernel.php` with `$schedule->command('certificate:check')->daily()`. Use the package’s methods like `daysUntilExpirationDate()` to trigger alerts when certificates expire soon.
- Does this package support wildcard certificates (e.g., *.example.com)?
- Yes, the package handles wildcard certificates automatically. Use `createForHostName('*.example.com')` to validate them. The `getSubject()` method will return the full subject, including wildcards, for further processing if needed.
- How do I test this package in a Laravel project with mocked network responses?
- Mock `stream_socket_client()` in your tests to avoid flaky network calls. Use Pest or PHPUnit to simulate certificate responses, including edge cases like expired or self-signed certs. Test against `badssl.com` for real-world scenarios.
- Are there alternatives to spatie/ssl-certificate for Laravel?
- Alternatives include `paragonie/easy-pki` (for PKI tasks) or `phpseclib/phpseclib` (for low-level SSL checks). However, Spatie’s package is Laravel-optimized, lightweight, and focused on certificate validation with a fluent interface, making it ideal for most use cases.
- How do I extend this package to validate OCSP stapling or CRL checks?
- The package doesn’t include OCSP/CRL checks, but you can extend it by adding custom methods. Use PHP’s `openssl_crl_match()` or `openssl_ocsp_check()` functions in a wrapper class. Example: Create a `CertificateValidator` facade that extends `SslCertificate` with these checks.