How can PHP developers handle certificate failures when connecting to an Exchange server?

When connecting to an Exchange server using PHP, developers may encounter certificate failures due to mismatched or expired certificates. To handle this issue, developers can disable certificate verification or provide a custom certificate file for the connection. This approach should be used with caution as it may compromise the security of the connection.

// Disable certificate verification
$stream_context = stream_context_create([
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
    ],
]);

// Connect to Exchange server with disabled certificate verification
$exchange_url = 'https://exchange_server_url';
$exchange_stream = stream_socket_client($exchange_url, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $stream_context);

if (!$exchange_stream) {
    die("Failed to connect to Exchange server: $errstr ($errno)");
}

// Perform operations on Exchange server
// ...

// Close connection
fclose($exchange_stream);