What potential issues can arise when trying to establish an SSL connection in PHP?

One potential issue when establishing an SSL connection in PHP is encountering SSL certificate verification errors, such as "unable to get local issuer certificate" or "SSL certificate problem: unable to get local issuer certificate." This can happen if the server's SSL certificate is not properly configured or if the CA certificates are not correctly installed on the server. To solve this issue, you can manually set the path to the CA certificates file in your PHP code.

// Set the path to the CA certificates file
$cafile = '/path/to/ca-bundle.crt';

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

// Establish an SSL connection using the stream context
$fp = fopen('https://example.com', 'r', false, $context);

// Check if the connection was successful
if (!$fp) {
    echo "Failed to establish SSL connection";
} else {
    // Connection successful, do something with the data
    echo stream_get_contents($fp);
}

// Close the connection
fclose($fp);