File_get_contents cannot request a solution for https connection _ PHP Tutorial

Source: Internet
Author: User
File_get_contents cannot request a solution for https connection. Using file_get_contents to fail to request https connection issue. The prepare method is very simple. you only need to enable php_openssl. of course, you need to install the openssl module in linux. By default, PHP. ini is configured with file_get_contents, which cannot be used to request https connections. the restore method is very simple. you only need to enable php_openssl. of course, you need to install the openssl module in linux.

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:

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

Bytes. PHP. ini default configuration...

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.