How to fix file_get_contents(): SSL operation failed with code 1, Failed to enable crypto with PHP?

Spread the love

Sometimes, we want to fix file_get_contents(): SSL operation failed with code 1, Failed to enable crypto with PHP.

In this article, we’ll look at how to fix file_get_contents(): SSL operation failed with code 1, Failed to enable crypto with PHP.

How to fix file_get_contents(): SSL operation failed with code 1, Failed to enable crypto with PHP?

To fix file_get_contents(): SSL operation failed with code 1, Failed to enable crypto with PHP, we can disable verify SSL connection.

For instance, we write

<?php
$arrContextOptions = [
    "ssl" => [
        "verify_peer" => false,
        "verify_peer_name" => false,
    ],
];

$response = file_get_contents(
    "https://example.com",
    false,
    stream_context_create($arrContextOptions)
);

echo $response;

to call file_get_contents with the URL of the content to get and the object returned by stream_context_create($arrContextOptions).

In $arrContextOptions, we add the verify_peer and verify_peer_name entries to the associative array and set them both to false to disable verifying the SSL connection.

Disabling verifying SSL connection would make our code vulenrable to man in the middle attacks.

Conclusion

To fix file_get_contents(): SSL operation failed with code 1, Failed to enable crypto with PHP, we can disable verify SSL connection.

Leave a Reply

Your email address will not be published.