Spatie\SslCertificate\Downloader::downloadCertificateFromUrl PHP Метод

downloadCertificateFromUrl() публичный статический Метод

public static downloadCertificateFromUrl ( string $url, integer $timeout = 30 ) : array
$url string
$timeout integer
Результат array
    public static function downloadCertificateFromUrl(string $url, int $timeout = 30) : array
    {
        $hostName = (new Url($url))->getHostName();
        $streamContext = stream_context_create(['ssl' => ['capture_peer_cert' => true]]);
        try {
            $client = stream_socket_client("ssl://{$hostName}:443", $errorNumber, $errorDescription, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
        } catch (Throwable $thrown) {
            if (str_contains($thrown->getMessage(), 'getaddrinfo failed')) {
                throw CouldNotDownloadCertificate::hostDoesNotExist($hostName);
            }
            if (str_contains($thrown->getMessage(), 'error:14090086')) {
                throw CouldNotDownloadCertificate::noCertificateInstalled($hostName);
            }
            throw CouldNotDownloadCertificate::unknownError($hostName, $thrown->getMessage());
        }
        $response = stream_context_get_params($client);
        return openssl_x509_parse($response['options']['ssl']['peer_certificate']);
    }

Usage Example

Пример #1
0
 public static function createForHostName(string $url, int $timeout = 30) : SslCertificate
 {
     $rawCertificateFields = Downloader::downloadCertificateFromUrl($url, $timeout);
     return new static($rawCertificateFields);
 }