Created
May 5, 2021 07:27
-
-
Save aaronsummers/ad9ca79d21477cbfb342a8e9dcafb3db to your computer and use it in GitHub Desktop.
Fix file_get_contents() over ssl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For my use, I had to strip /blog from the SVG URL.
file_get_contents_ssl( str_replace('/blog', '', get_site_url()) . $menu_icon )