Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

file_get_contents fails with SSL operation failed, failed to enable crypto

Errorfile_get_contents(): SSL operation failed with code 1

Short 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.

  • Applies to PHP 8.3, PHP 8.4, OpenSSL 3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The URL uses an IP address or an alias, not the name printed on the certificateThe URL host does not match the certificate nameFix
    3. 3The target is an internal host or test server with its own certificateThe server uses a self-signed or private-CA certificateFix

What does the error look like?

  • file_get_contents(): SSL operation failed with code 1
  • file_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.crt

2Fix

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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.