How to get file_get_contents() to work with HTTPS?
When you switch from HTTP to HTTPS, PHP's file_get_contents() function doesn't automatically handle the certificate validation required for secure connections. If your code works perfectly on a test server using plain HTTP but fails immediately with "failed to open stream" when pointing at an HTTPS endpoint, the culprit is almost certainly SSL certificate verification.
The standard behavior of PHP is to verify the remote certificate against the system's trusted Certificate Authority (CA) bundle. When this verification failsâwhether due to a self-signed certificate, an expired cert, or a mismatched domain nameâthe request is aborted before any data is returned. This is exactly what happens in your current scenario where the test server didn't enforce SSL, but the production environment does.
To resolve this, you need to explicitly configure the stream context to handle SSL options. The most common approach for development or internal systems using self-signed certificates is to disable verification entirely. Here is how you modify your existing function:
function send($packet, $url) {
$ctx = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $packet,
'ignore_errors' => true // Optional: helps if the server responds with an error status code
]
]);
return file_get_contents($url, false, $ctx);
}
By setting verify_peer to false, you tell PHP not to check the certificate's validity. Setting verify_peer_name to false prevents PHP from checking if the certificate matches the hostname you are connecting to. This bypasses the security checks that cause the stream failure.
However, there is a significant trade-off here. Disabling peer verification exposes your application to Man-in-the-Middle (MitM) attacks. An attacker on the same network could intercept your traffic and read or modify sensitive data like credit card numbers if they present a forged certificate. Therefore, this configuration should strictly be used in non-production environments or when you have a specific need to connect to an internal service that does not use a public CA-signed certificate.
If you are connecting to a legitimate third-party payment processor that requires HTTPS, disabling verification is generally a bad practice for production. In those cases, the issue is likely that your server lacks the root certificates required to validate the provider's chain of trust. You can resolve this without disabling security by updating the CA bundle on your system.
On Linux systems, you might need to ensure the ca-certificates package is up to date or copy the contents of /etc/pki/tls/certs/ca-bundle.crt (or /etc/ssl/certs/ca-certificates.crt) into a location PHP can access. You can then point your stream context to this specific file:
$ctx = stream_context_create([
'ssl' => [
'cafile' => '/path/to/local-ca-bundle.crt',
'capath' => '/path/to/local-ca-certs/'
],
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => $packet,
]
]);
This approach maintains the security benefits of certificate validation while allowing you to use a custom CA bundle if your system is missing specific roots. For production credit card processing, this method is far superior to disabling verification. If you are using a framework like Laravel, ensure your php.ini or local configuration loads the correct CA bundle automatically, as many shared hosting environments handle this by default.
Remember that file_get_contents() is convenient but lacks the retry logic and error handling capabilities of cURL. For mission-critical financial transactions, consider migrating to cURL or a dedicated HTTP client which offers better control over timeouts, redirects, and error responses.
