What are the best practices for handling SSL certificate warnings in PHP applications?

When handling SSL certificate warnings in PHP applications, it is important to verify the SSL certificate to ensure secure communication between the client and server. One common approach is to disable SSL verification altogether, but this can pose a security risk. A better practice is to properly configure PHP to trust the SSL certificate provided by the server.

// Create a stream context with SSL verification
$context = stream_context_create([
    'ssl' => [
        'verify_peer' => true,
        'verify_peer_name' => true,
        'allow_self_signed' => false
    ]
]);

// Make a request using the stream context
$response = file_get_contents('https://example.com', false, $context);

// Check for SSL errors
if ($response === false) {
    $error = error_get_last();
    echo "SSL error: " . $error['message'];
}