Solution: file_get_contents cannot request https connection
In the default configuration of PHP. ini, when file_get_contents is used to read the https link, the following error is reported. This article provides a solution.
Error: Warning: fopen () [function. fopen]: Unable to find the wrapper "https"-did you forget to enable it when you configured PHP?
Solution 3:
1. For PHP in windows, you only need to go to php. ini and delete the extension = php_openssl.dll. Then restart the service.
2. For PHP in linux, you must install the openssl module. After installation, you can access it.
3. If you cannot modify the configuration on the server, use the curl function to replace the file_get_contents function. Of course it is not a simple replacement. The curl function can be used properly only with corresponding parameter configurations.
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 (! Emptyempty ($ 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;
}