Home > Backend Development > PHP Tutorial > How to Fix 'SSL operation failed with code 1' Errors When Using `file_get_contents()` in PHP?

How to Fix 'SSL operation failed with code 1' Errors When Using `file_get_contents()` in PHP?

Barbara Streisand
Release: 2024-12-25 10:31:17
Original
626 people have browsed it

How to Fix

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");
Copy after login

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
Copy after login

Solution:

To resolve this issue, the user implemented the following steps:

  1. Referenced the PHP 5.6 OpenSSL migration documentation at http://php.net/manual/en/migration56.openssl.php.
  2. Learned about the additional "verify_peer_name" parameter that needed to be set to false.
  3. Adjusted their PHP code to include the updated parameters:
$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));
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template