File_get_contents cannot request https connection solution

Source: Internet
Author: User
Tags curl ini openssl openssl library

In the default configuration of PHP. ini, the following error occurs when file_get_contents is used to read the https link:

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 needIn php. ini, set extension = php_openssl.dllDelete the previous one. Restart the service.
2. For PHP in linux, you must install the openssl module. After installation, you can access it.

Installation method


OpenSSL library installation

Official website: http://www.openssl.org

Download Page: http://www.openssl.org/source/

Download the latest version

Http://www.openssl.org/source/openssl-1.0.0a.tar.gz

Decompress:

Tar-zxvf openssl-1.0.0d.tar.gz, unzipped Directory: openssl-1.0.0d

Then enter the cd openssl-1.0.0d for configuration, compilation, installation

Configuration

./Configure or./config

Compile

Make

Install

Make install

 

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: 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;
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.