file_get_contents fails in MAMP with self signed certificate

Viewed 770

I'm including svg files in my site using file_get_contents(). Normally this works fine on http, but I setup this site to use https locally. When I do I get this error:

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed on line 226

Warning: file_get_contents(): Failed to enable crypto on line 226

From what I can tell, it's because the cert is self signed. I found a local solution of disabling the SSL check on file get contents. I found something similar using cURL. However this seems to be a security hole. Is there a way to make this work without disabling SSL checks in file_get_contents or cURL?

2 Answers

You need allow to use a self-signed certificate. For that you need to define a custom stream context.

Example:

file_get_contents(
    'https://self-signed.badssl.com/',
    false,
    stream_context_create([
        'ssl' => [
            'allow_self_signed'=> true
        ]
    ])
);

You might also try upgrading your version of MAMP. This worked for me. I was two releases behind on MAMP while running it on MacOS Catalina and ran into a similar SSL error and finally solved it by just upgrading MAMP after wasting lots of time reading various Stackoverlow threads for a solution to no avail.

Related