Skip to content

Instantly share code, notes, and snippets.

@mindpalette
Created November 26, 2019 20:26
Show Gist options
  • Save mindpalette/edd2c7f72a3295583d8441aa9d8e8655 to your computer and use it in GitHub Desktop.
Save mindpalette/edd2c7f72a3295583d8441aa9d8e8655 to your computer and use it in GitHub Desktop.
<?php
// file_get_contents wrapper to allow self-signed certs for local dev domains
function contextual_file_get_contents( $url ) {
$server_name = (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : '';
$localext = '.local';
if ($server_name && substr($server_name, -strlen($localext)) === $localext) {
$context = stream_context_create(['ssl' => [
'verify_peer_name'=>false,
'verify_peer'=>false,
'allow_self_signed'=> true
] ]);
return file_get_contents($url, false, $context);
} else return file_get_contents($url);
}
@mindpalette
Copy link
Author

Workaround for SSL errors when using file_get_contents on MAMP local environment (file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed). Assumes local dev domains have ".local" extension on line 6 (change as needed, or use full dev domain name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment