Maaz Zindani

Loading the index…

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

Guzzle refuses an HTTPS request because the certificate cannot be verified

ErrorSSL certificate problem: unable to get local issuer certificate

Short answer

Guzzle verifies certificates by default, using the operating system's CA bundle. When PHP cannot find a bundle, or the server uses a private CA, the request fails. Point verify at a CA file, or set openssl.cafile in php.ini. verify => false switches the check off and is unsafe outside a throwaway test.

  • Applies to Guzzle 7, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which of the two situations you are in: a public site whose chain your machine cannot check, or a private or self-signed server. A browser or curl against the same URL tells you.

    curl -vI https://example.com 2>&1 | grep -i -E "SSL|certificate|verify"
    php -r "echo ini_get('openssl.cafile'), PHP_EOL;"
  2. 2Match what you see, then jump to its fix

    1. 1Public sites fail the same way from PHP, and the openssl.cafile setting prints nothing.PHP has no CA bundle to verify againstFix
    2. 2Only your internal host fails, and browsers on managed machines trust it while PHP does not.The server uses a private or self-signed certificate authorityFix
    3. 3A development box with a throwaway certificate and no real data.You only need it off for a local testFix

What does the error look like?

  • SSL certificate problem: unable to get local issuer certificate
  • How to ignore invalid SSL certificate errors in Guzzle

Cause 1 of 3

PHP has no CA bundle to verify against

1Check

Guzzle's verify option is true by default and uses the default CA bundle of the operating system. Where PHP has none, every HTTPS request fails.

php -r "echo ini_get('openssl.cafile'), PHP_EOL;"

2Fix

Download a CA bundle (Guzzle's docs link the one cURL's maintainer publishes) and set openssl.cafile in php.ini to its path. Restart the web server or PHP-FPM afterwards. This fixes every request without a code change.

; php.ini
openssl.cafile=/etc/ssl/certs/ca-bundle.pem

3Verify

Run a request through Guzzle with no verify option.

php -r "require 'vendor/autoload.php'; echo (new GuzzleHttp\Client)->get('https://example.com')->getStatusCode(), PHP_EOL;"

Confirmed by docs.guzzlephp.org

Did this fix it?

Cause 2 of 3

The server uses a private or self-signed certificate authority

1Check

Verify accepts a string path to a CA bundle, so PHP can be told to trust a specific certificate.

openssl s_client -connect internal.example.com:443 -showcerts </dev/null

2Fix

Pass the path of the CA certificate for that client, so verification stays on.

$client = new GuzzleHttp\Client([
    'verify' => '/path/to/internal-ca.pem',
]);
$client->request('GET', 'https://internal.example.com/');

3Verify

The request now returns a status code instead of throwing.

php -r "require 'vendor/autoload.php'; echo (new GuzzleHttp\Client(['verify' => '/path/to/internal-ca.pem']))->get('https://internal.example.com/')->getStatusCode();"

Confirmed by docs.guzzlephp.org

Did this fix it?

Cause 3 of 3

You only need it off for a local test

1Check

The Guzzle docs mark disabling validation as insecure and label the example do not do this.

grep -rn "'verify'" app/ src/ 2>/dev/null

2Fix

If you must, switch the check off only for that client in a development environment, and remove it before shipping.

$client = new GuzzleHttp\Client([
    'verify' => getenv('APP_ENV') !== 'local',
]);

3Verify

Search the code base for any verify => false before a release.

grep -rn "verify.*false" app/ src/ 2>/dev/null

Confirmed by docs.guzzlephp.org

Did this fix it?

How do I stop it coming back?

  • Keep one CA bundle path in php.ini on every server instead of per-call options.
  • Never ship verify => false; treat a grep hit in code review as a bug.
  • Add your internal CA to the machine's trust store, so tools other than PHP agree.

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.