Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Created May 5, 2021 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronsummers/ad9ca79d21477cbfb342a8e9dcafb3db to your computer and use it in GitHub Desktop.
Save aaronsummers/ad9ca79d21477cbfb342a8e9dcafb3db to your computer and use it in GitHub Desktop.
Fix file_get_contents() over ssl
<?php
/**
* File get contents fails without certificate on HTTPS
*
* https://stackoverflow.com/questions/26148701/file-get-contents-ssl-operation-failed-with-code-1-failed-to-enable-crypto#answer-40311146
*/
function file_get_contents_ssl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000); // 3 sec.
curl_setopt($ch, CURLOPT_TIMEOUT, 10000); // 10 sec.
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
@aaronsummers
Copy link
Author

For my use, I had to strip /blog from the SVG URL.
file_get_contents_ssl( str_replace('/blog', '', get_site_url()) . $menu_icon )

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