Troubleshooting file_get_contents() SSL Certificate Verification Error
When attempting to access a REST service using file_get_contents() in PHP 5.6, you may encounter the error "SSL operation failed with code 1" due to stricter certificate verification.
Issue Description
The user's PHP page tries to fetch data from an HTTPS endpoint using file_get_contents(). However, it fails with the following errors:
Root Cause
PHP 5.6 introduced stricter SSL verification by default, requiring accurate certificate validation. The error indicates that the client cannot verify the certificate of the remote server.
Resolution
To resolve this issue, an official PHP document on OpenSSL changes in 5.6 recommends disabling certificate verification. Note: this solution has significant security implications and should only be considered in controlled environments where trust between client and server is established.
$arrContextOptions = array( "ssl" => array( "verify_peer" => false, "verify_peer_name" => false, ), ); $response = file_get_contents("remote_url", false, stream_context_create($arrContextOptions));
Important Note
Disabling SSL certificate verification reduces security and can expose your application to eavesdropping and other security breaches. It is strongly recommended to configure your system correctly to use trusted SSL certificates instead of resorting to this workaround.
The above is the detailed content of How to Fix 'SSL operation failed with code 1' Error in PHP's `file_get_contents()`?. For more information, please follow other related articles on the PHP Chinese website!