file_get_contents fails with SSL operation failed, failed to enable crypto
file_get_contents(): SSL operation failed with code 1Short answer
PHP checks the server certificate and the host name by default (verify_peer and verify_peer_name are true). The call fails when PHP cannot find a CA bundle, when the name in the certificate does not match the host you used, or when the server presents a certificate PHP does not trust. Fix the CA file, not the check.
Fix it fast
1Find out which cause you have
Print where PHP looks for CA certificates and which file is configured. An empty openssl.cafile with no usable system path is the most common reason.
php -r 'print_r(openssl_get_cert_locations());' php -i | grep -i "openssl.cafile\|openssl.capath"2Match what you see, then jump to its fix
- 1
The same URL works from a browser or the curl tool, but not from PHP, and openssl.cafile is emptyPHP has no CA bundle that contains the issuerFix - 2
The URL uses an IP address or an alias, not the name printed on the certificateThe URL host does not match the certificate nameFix - 3
The target is an internal host or test server with its own certificateThe server uses a self-signed or private-CA certificateFix
- 1
What does the error look like?
file_get_contents(): SSL operation failed with code 1file_get_contents(): Failed to enable crypto
Cause 1 of 3
PHP has no CA bundle that contains the issuer
1Check
PHP verifies the peer against the file in openssl.cafile first, then the hashed directory in openssl.capath. Both default to empty. The first command shows which locations are in use and whether the file exists.
php -r 'print_r(openssl_get_cert_locations());'
ls -l /etc/ssl/certs/ca-certificates.crt2Fix
Point php.ini at a current CA bundle. The directives cannot be changed with ini_set(), so edit php.ini and restart PHP. For one call only, pass the file in a stream context.
; php.ini
openssl.cafile = "/etc/ssl/certs/ca-certificates.crt"
// one call only
$ctx = stream_context_create(['ssl' => ['cafile' => '/etc/ssl/certs/ca-certificates.crt']]);
$data = file_get_contents('https://example.com/api', false, $ctx);3Verify
Run the request again and confirm it returns data.
php -r 'var_dump(strlen(file_get_contents("https://example.com")));'Confirmed by php.net
Cause 2 of 3
The URL host does not match the certificate name
1Check
With verify_peer_name on, PHP compares the certificate with the host in the URL. Opening the server by IP address, or through an alias the certificate does not list, fails.
grep -rn "https://[0-9]" src/ app/2Fix
Use a host name that the certificate lists. If you must connect through another address, set peer_name to the name on the certificate rather than switching verification off.
$ctx = stream_context_create(['ssl' => ['peer_name' => 'api.example.com']]);
$data = file_get_contents('https://203.0.113.10/api', false, $ctx);3Verify
Repeat the request and confirm no SSL warning appears.
php -r 'var_dump(file_get_contents("https://api.example.com/", false, null, 0, 100));'Confirmed by php.net
Cause 3 of 3
The server uses a self-signed or private-CA certificate
1Check
A self-signed certificate is refused while allow_self_signed is false (the default). That option only works together with verify_peer.
curl -vI https://internal.example/ 2>&1 | grep -i "issuer\|subject"2Fix
Trust that certificate explicitly: give its file as cafile and keep verify_peer on. Do not set verify_peer to false in production, as that removes the identity check.
$ctx = stream_context_create(['ssl' => [
'cafile' => '/etc/ssl/private-ca.pem',
'verify_peer' => true,
]]);
$data = file_get_contents('https://internal.example/api', false, $ctx);3Verify
Run the call and confirm it returns the response body.
php -r 'echo strlen(file_get_contents("https://internal.example/", false, stream_context_create(["ssl"=>["cafile"=>"/etc/ssl/private-ca.pem"]])));'Confirmed by php.net
How do I stop it coming back?
- Keep the system CA package current and point openssl.cafile at it on servers and containers.
- Never ship verify_peer => false or verify_peer_name => false; give the right CA or name instead.
- Call external APIs with a cURL wrapper that logs the exact SSL error text.
- Check certificate expiry of the services you call.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP says it cannot find the https wrapper
Warning: fopen(): Unable to find the wrapper "https"
PHP registers the https:// wrapper only when its openssl extension is loaded.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix