[Solved] PHP Error: Warning: file_get_contents(): Failed to enable crypto

I. Description
file_get_contents() error when reading https content

II. Reason
Local service is not configured ssl certificate, can not get the contents of the path to https

III. Solution
1, Linux server configuration https ssl certificate.
Linux server configuration https ssl certificate.
2. curl_ request to obtain the content (see the following method curlGet ())

    /**
     * get the content
     * @param $url
     * @return bool|string
     */
    function curlGet($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $content = curl_exec($ch);
        curl_close($ch);
        return ($content);
    }

3.Use file_get_contents() function to skip https authentication

        $streamOpts = [
            'ssl' => [
                'verify_peer' => false,
                'verify_peer_name' => false
            ]
        ];
        $html = file_get_contents($pageUrl, false, stream_context_create($streamOpts));

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *