Solution: file_get_contents cannot request https connection

Source: Internet
Author: User

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;

}

 

Related Article

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.