PHP captures HTTPS webpage content methods and error handling recently encountered an HTTPS problem when studying HackerNewsAPI. Because all hackernewsapis are accessed through the encrypted HTTPS protocol, unlike the common HTTP protocol, when the file_get_contents () function in PHP is used to obtain the data provided in the API, an error occurs. the code used is as follows: & lt ;? Php $ data & nbsp; file PHP methods for capturing HTTPS webpage content and error handling
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 the file_get_contents () function in PHP is used to obtain the data provided in the API, if an error occurs, the code used is as follows:
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?
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: nohttp wrapper: yeshttps wrapper: nowrappers: 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;}