PHP function file_ get_ Contents() reports an error when using HTTPS protocol: SSL operation failed

Scenario:

file_ get_ The contents () function is used to read the contents of a file into a string. It is one of the commonly used functions to read the contents of a file.

But sometimes file is used on the server_ get_ When the contents() function requests the URL file of HTTPS protocol, an error will be reported, and the file content cannot be read correctly,

reason:

The server is not properly configured with HTTPS certificate

Solution: (three solutions)

Method 1:

Download HTTPS certificate to server

The server downloads this certificate, http://curl.haxx.se/ca/cacert.pem
Php.ini configuration
openssl.cafile = “/ etc/SSL/certs/cacert. PEM”// the path where you actually download the certificate
Restart PHP

Method 2:

Use the curl function to process HTTPS parameters and obtain the file content

<?php
function getSSLPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSLVERSION,3); 
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

var_dump(getSSLPage("https://xxx.xxx.xxx"));
?>

Method 3:

Make file_ get_ The contents() function skips HTTPS authentication

$stream_opts = [
    "ssl" => [
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ]
]; 

$response = file_get_contents("https://xxx.xxx.xxx",false,stream_context_create($stream_opts));

  It is recommended to use curl function instead of file in development_ get_ Contents() function.

Read More: