Troubleshooting the PHP file_get_contents SSL3_GET_SERVER_CERTIFICATE certificate verify failed error in macOS and Mac OS X

El Capitan

I am starting to feel that Apple is not at all interested in building environments that are friendly for web developers. They place key dependencies in non-standard locations and most recently, they have decided to not preload OpenSSL in macOS in favour of their own incomplete SSL library, which has caused headaches for me and probably thousands of other web developers who use macOS.

One recent problem I encountered that resulted from the missing OpenSSL library is this. When using PHP to download remote files over HTTPS, one can get an error message that says something like “SSL3_GET_SERVER_CERTIFICATE certificate verify failed error”. This error message probably appears because the built-in SSL library in macOS does not recognize a particular signing authority.

The easiest but worst workaround, which I am sharing with you, is just to disable the SSL verification process with this:

$options = array(
   "ssl" => array(
      "verify_peer" => false,
      "verify_peer_name" => false,
   ),
);
file_get_contents($url, false, stream_context_create($options));

This solution is suboptimal because disabling the SSL verification process defeats the purpose of connecting via SSL. The optimal solution would be to add the missing SSL libraries to your Mac, but yeah, I am not doing it, stupid Apple.