file_get_Contents and fopen return false when requesting HTTPS address: Error content: SSL routes: ssl3_get_server_certificate:certificate verify failed

Recently found file in project_get_Contents, fopen suddenly doesn’t work?

After troubleshooting, it is found that the access can be successful by using HTTP access, and an error will be reported with s and false will be returned;

At first, I thought it was an SSL certificate problem, so I changed the certificate and operated again; False is returned. Detailed error log: SSL routes: ssl3_get_server_certificate:certificate verify failed;

Still not;

file_get_Contents can be solved in the following three ways:

1. Modify the php.ini configuration file

For PHP under windows, just go to php.ini and delete the front of extension=php_openssl.dll; and restart the service. (Note that allow_url_fopen must also be turned on)

For PHP under Linux, you must install the OpenSSL module. After installation, you can access it.

2.stream_context_Create method

$url= 'https://example.com';
$arrContextOptions=array(
      "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );  
$response = file_get_contents($url, false, stream_context_create($arrContextOptions));

3. Replace file with curl function_ get_ contents

function getSslPage($url) {
    /*  http://www.manongjc.com/article/1428.html */
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

Use method 2 to solve the file_get_contents problem temporarily, but I always feel something is wrong, because it can be accessed before;

Later, fopen reported an error of false;

Keep looking for information……

This is because PHP cannot verify the certificate. Look at phpinfo and find openssl.cafile, which can be found in the php.ini file;

The certificate exists. It should be expired. Replace the certificate.

You can download the latest CA certificate at the following address
https://curl.haxx.se/ca/cacert.pem

Replace the certificate and reload the PHP configuration; Perfect solution!!!

Similar Posts: