This article describes how to capture HTTPS content in PHP and how to solve the problem. For more information, see
This article describes how to capture HTTPS content in PHP and how to solve the problem. For more information, see
I recently encountered an HTTPS problem when studying the Hacker News API. Because all Hacker News APIs are accessed through the encrypted HTTPS protocol, unlike the common HTTP protocol, when using the file_get_contents () function in PHP () an error occurs when you obtain the data provided by the API. The code used is as follows:
<? Php
$ Data = file_get_contents ("");
......
When the above Code is run, the following error message is displayed:
PHP Warning: file_get_contents (): Unable to find the wrapper "https"-did you forget to enable it when you configured PHP?
Below is:
Why is this error?
After some searches on the Internet, I found that there were still many people who encountered such errors. The problem was very direct because a parameter was not enabled in the PHP configuration file, on my local machine, it is/apache/bin/php. extension = php_openssl.dll in ini. Remove the semicolon. You can use the following script to check the configuration of your PHP environment:
$ W = stream_get_wrappers ();
Echo 'openssl: ', extension_loaded ('openssl ')? 'Yes': 'no', "\ n ";
Echo 'HTTP wrapper: ', in_array ('http', $ w )? 'Yes': 'no', "\ n ";
Echo 'https wrapper: ', in_array ('https', $ w )? 'Yes': 'no', "\ n ";
Echo 'wrappers: ', var_dump ($ w );
Run the above script snippet and the result on my machine is:
Openssl: no
Http wrapper: yes
Https wrapper: no
Wrappers: array (10 ){
[0] =>
String (3) "php"
[1] =>
String (4) "file"
[2] =>
String (4) "glob"
[3] =>
String (4) "data"
[4] =>
String (4) "http"
[5] =>
String (3) "ftp"
[6] =>
String (3) "zip"
[7] =>
String (13) "compress. zlib"
[8] =>
String (14) "compress.bzip2"
[9] =>
String (4) "phar"
}
Alternative
It is very easy to find and correct errors. However, it is difficult to correct errors after they are found. I originally wanted to run this script on a remote host, but I was unable to modify the PHP configuration of the remote host. As a result, I was not able to use this scheme, but we could not throw it on a tree, there is no way through this road to see if there is any other way.
Another function that I often use to capture content in PHP is curl, which is more powerful than file_get_contents () and provides many optional parameters. To access HTTPS content, we need to use the CURL configuration parameters:
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
You can see from the semantics that it ignores/skips SSL security authentication. This may not be a good practice, but it is sufficient for common scenarios.
The following is a function encapsulated by Curl that can access HTTPS content:
Function getHTTPS ($ url ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_HEADER, false );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_REFERER, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );
Return $ result;
}
The above is the entire process for php to obtain https content. It is very simple and practical. We recommend it to partners who have the same project requirements.