file_get_contents(): SSL operation failed with code 1, Failed to enable crypto

前端 未结 16 1788
情歌与酒
情歌与酒 2020-11-22 04:31

I’ve been trying to access this particular REST service from a PHP page I’ve created on our server. I narrowed the problem down to these two lines. So my PHP page looks li

相关标签:
16条回答
  • 2020-11-22 04:57

    Just wanted to add to this since I ran into the same problem and nothing I could find anywhere would work (e.g downloading the cacert.pem file, setting cafile in php.ini etc.)

    If you are using NGINX and your SSL certificate comes with an "intermediate certificate", you need to combine the intermediate cert file with your main "mydomain.com.crt" file and it should work. Apache has a setting specific for intermediate certs, but NGINX does not so it must be within same file as your regular cert.

    0 讨论(0)
  • 2020-11-22 04:57

    After falling victim to this problem on centOS after updating php to php5.6 I found a solution that worked for me.

    Get the correct directory for your certs to be placed by default with this

    php -r 'print_r(openssl_get_cert_locations()["default_cert_file"]);'
    

    Then use this to get the cert and put it in the default location found from the code above

    wget http://curl.haxx.se/ca/cacert.pem -O <default location>
    
    0 讨论(0)
  • 2020-11-22 04:57

    I had the same issue for another secure page when using wget or file_get_contents. A lot of research (including some of the responses on this question) resulted in a simple solution - installing Curl and PHP-Curl - If I've understood correctly, Curl has the Root CA for Comodo which resolved the issue

    Install Curl and PHP-Curl addon, then restart Apache

    sudo apt-get install curl
    sudo apt-get install php-curl
    sudo /etc/init.d/apache2 reload
    

    All now working.

    0 讨论(0)
  • 2020-11-22 05:01

    You can get around this problem by writing a custom function that uses curl, as in:

    function file_get_contents_curl( $url ) {
    
      $ch = curl_init();
    
      curl_setopt( $ch, CURLOPT_AUTOREFERER, TRUE );
      curl_setopt( $ch, CURLOPT_HEADER, 0 );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
      curl_setopt( $ch, CURLOPT_URL, $url );
      curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, TRUE );
    
      $data = curl_exec( $ch );
      curl_close( $ch );
    
      return $data;
    
    }
    

    Then just use file_get_contents_curl instead of file_get_contents whenever you're calling a url that begins with https.

    0 讨论(0)
提交回复
热议问题