Skip to content
PHP Logo

What potential issues can arise when trying to use file_get_contents with HTTPS in PHP?

When using file_get_contents with HTTPS in PHP, potential issues can arise due to SSL verification problems. This can result in errors such as "SSL certificate problem: unable to get local issuer certificate" or "failed to open stream: operation failed". To solve this, you can disable SSL verification by setting the "verify_peer" and "verify_peer_name" options to false in a stream context.

php
$context = stream_context_create([
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false
    ]
]);

$url = 'https://example.com';
$data = file_get_contents($url, false, $context);

if ($data === false) {
    echo 'Failed to retrieve data from ' . $url;
} else {
    echo $data;
}