The solution to the problem of being unable to request https connection using file_get_contents is very simple. We only need to turn on php_openssl. Of course, the Linux system needs to install the openssl module.
Under the default configuration of PHP.ini, using file_get_contents to read https links will result in the following error:
Warning: fopen() [function.fopen]: Unable to find the wrapper “https” – did you forget to enable it when you configured PHP?
There are 3 solutions:
1. For PHP under windows, just go to php.ini, delete the ; in front of extension=php_openssl.dll, and restart the service.
2. For PHP under Linux, you must install the openssl module. After installation, you can access it.
Installation method
Installation of OpenSSL library
Official website: http://www.openssl.org
Download page: http://www.openssl.org/source/
Select the latest version to download
http://www.openssl.org/source/openssl-1.0.0a.tar.gz
Unzip:
tar –zxvf openssl-1.0.0d.tar.gz, the decompression directory is: openssl-1.0.0d
Then enter cd openssl-1.0.0d to configure, compile and install
Configuration
./configure or ./config
Compile
make
Installation
make install
3. If you cannot modify the configuration of the server, then use the curl function to replace the file_get_contents function. Of course, it is not a simple replacement. There are also corresponding parameter configurations to use the curl function normally.
The curl function is encapsulated as follows:
The code is as follows
代码如下 |
复制代码 |
function http_request($url,$timeout=30,$header=array()){
if (!function_exists('curl_init')) {
throw new Exception('server not install curl');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$data = curl_exec($ch);
list($header, $data) = explode("rnrn", $data);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
$matches = array();
preg_match('/Location:(.*?)n/', $header, $matches);
$url = trim(array_pop($matches));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
}
if ($data == false) {
curl_close($ch);
}
@curl_close($ch);
return $data;
}
|
|
Copy code |
|
function http_request($url,$timeout=30,$header=array()){
If (!function_exists('curl_init')) {
throw new Exception('server not install curl');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$data = curl_exec($ch);
list($header, $data) = explode("rnrn", $data);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
If ($http_code == 301 || $http_code == 302) {
$matches = array();
preg_match('/Location:(.*?)n/', $header, $matches);
$url = trim(array_pop($matches));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
}
if ($data == false) {
curl_close($ch);
}
@curl_close($ch);
return $data;
}
http://www.bkjia.com/PHPjc/632728.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632728.htmlTechArticleUnable to request https connection problem using file_get_contents. The method is very simple. We only need to turn on php_openssl. Of course, the Linux system The openssl module needs to be installed. PHP.ini is configured by default...