Calling file_get_contents() with https:// urls give me the following error:
warning: file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?
I've read 10+ SO questions and all of them say to enable extension=php_openssl.dll
I did this and I'm still having issues...
What else could it be?
SOLVED
To solve this error, you need to install the OpenSSL
package for PHP on your webserver.
On a FreeBSD server, you may need to install the following package: php53_openssl
and restart your webserver.
On a Windows server, open your php.ini
config file and simply uncomment the following line:
;extension=php_openssl
and restart the webserver. The error should be resolved.
Reference: I leart it from this link which worked for me.., so credit goes to him.
For PHP 7 on Windows 64bit be sure to put libcrypto-1_1-x64.dll
to the environmental variable path. For example: C:\php\libcrypto-1_1-x64.dll;
Please ensure if allow_url_include and allow_url_fopen are set to 'On' in php.ini.
Uncomment them if these lines are commented.
Also, you can use the following script for diagnostics: (taken from the accepted answer on How to get file_get_contents() to work with HTTPS?)
$w = stream_get_wrappers();
echo 'openssl: ', extension_loaded ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_dump($w);
Like people say, check with the configuration.
phpinfo.php
<?php
phpinfo();
?>
Search for OpenSSL on the webpage.
Also, don't forget to restart the WebServer after altering the php.ini file.
If you can't use file_get_contents(), use cURL instead if available, it's better in many ways and faster.
function url($url,$option = null) {
$cURL = curl_init();
if ($option) {
curl_setopt($cURL, CURLOPT_URL, $url.$option);
} else {
curl_setopt($cURL, CURLOPT_URL, $url);
}
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL, CURLOPT_AUTOREFERER, 1);
curl_setopt($cURL, CURLOPT_HTTPGET, 1);
curl_setopt($cURL, CURLOPT_VERBOSE, 0);
curl_setopt($cURL, CURLOPT_HEADER, 0);
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($cURL, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($cURL, CURLOPT_DNS_USE_GLOBAL_CACHE, 0);
curl_setopt($cURL, CURLOPT_DNS_CACHE_TIMEOUT, 2);
$output['page'] = curl_exec($cURL);
$output['contentType'] = curl_getinfo($cURL, CURLINFO_CONTENT_TYPE);
curl_close($cURL);
return $output;
}
$page = url('https://example.com/','i/like/subfolders');
I just wanted to add this tidbit because I struggled for hours with this issue and finally figured out something that works. With a lot of these WAMP/XAMP/MAMP installations, the configuration files are placed in non-standard directories (i.e., C:\MAMP\conf\php\php7.2.10\php.ini). You need to make sure that your php cli has loaded the INI you are modifying.
$ php --ini
If it is not pointing to the correct INI, or loads no configuration file at all, you can change the PHPRC environment variable for the user executing the script to point to the correct location for the INI that you have correctly modified. Also, ensure that the extension_dir parameter in php.ini is contains the correct path to where your extensions are stored.
Hope this helps.
I had the same problem. I was trying to access facebook graph. It wasn't that it could not access the URL, it was that Facebook was returning a 400 error because I wasn't passing parameters through. Try connecting to HTTPS on a site that will have a working HTTPS connection. E.g. Try this:
file_get_contents("https://www.namhost.com");
If that works, you must see why the HTTPs connection you are connecting to is failing, because the problem isn't that you can't connect to HTTPs, but rather that what you are connecting to isn't liking the request.