Troubleshooting SSL Issues with file_get_contents()
In PHP 5.6, changes were introduced to OpenSSL handling that have occasionally led to errors like "SSL operation failed with code 1." One such instance, as reported by a user, occurred when attempting to access a REST service using file_get_contents().
Problem Description:
The user experienced an error while attempting to retrieve data from a REST service via file_get_contents():
$response = file_get_contents("https://maps.co.weber.ut.us/arcgis/rest/services/SDE_composite_locator/GeocodeServer/findAddressCandidates?Street=&SingleLine=3042+N+1050+W&outFields=*&outSR=102100&searchExtent=&f=json");
The error message displayed:
Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Solution:
To resolve this issue, the user implemented the following steps:
$arrContextOptions=array( "ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false, ), ); $response = file_get_contents("https://maps.co.weber.ut.us/arcgis/rest/services/SDE_composite_locator/GeocodeServer/findAddressCandidates?Street=&SingleLine=3042+N+1050+W&outFields=*&outSR=102100&searchExtent=&f=json", false, stream_context_create($arrContextOptions));
Important Note:
The user acknowledged the security implications of disabling SSL verification. This should only be done if the user fully understands the risks and has no other viable configuration options. For optimal security, enable SSL certificate verification by default.
The above is the detailed content of How to Fix 'SSL operation failed with code 1' Errors When Using `file_get_contents()` in PHP?. For more information, please follow other related articles on the PHP Chinese website!